{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# API 文档介绍（Azure API）\n",
    "* 本周主要内容：API文档阅读介绍及计算机视觉入门（认知服务）\n",
    "* 20春_API_人工智能与机器学习_week02\n",
    "*  电子讲义设计者：许智超，廖汉腾\n",
    "<br/>\n",
    "<br/>\n",
    "\n",
    "![app_交互设计](https://pic2.zhimg.com/v2-6c478325d02ca3363ec1817a952d5321_r.jpg)\n",
    "\n",
    "-----\n",
    "![app_API_交互设计](http://coverall1.splunk.com/web_assets/developers/devguide/Essentials02_Splunk_app_architecture.png)\n",
    "\n",
    "## 复习\n",
    "\n",
    "复习1：上周内容，AI、API、机器学习的基本认知以及数据科学的基本流程  \n",
    "\n",
    "* 1、API for AI 有哪些人工智能的分类（计算机视觉、语义识别、自然语言处理、推荐系统）\n",
    "* 2、机器学习的关键输入是什么？（有一定规范的数据）\n",
    "* 3、数据科学的基本流程（四个循环环节:问题定义->ETL和特征值提取->学习（机器）->模型部署）\n",
    "\n",
    "复习2：jupyter的基本使用，json、requests  主要包括以下内容可能涉及的API操作：\n",
    "\n",
    "* 1、requests POST传递参数 的基础用法\n",
    "* 2、JSON 格式的基本转换（字典）\n",
    "* 3、字典的处理，pandas转化数据结构\n",
    "<br/>\n",
    "<br/>\n",
    "\n",
    "\n",
    "-----\n",
    "\n",
    "![Face_识别示例](http://q7rdfqeve.bkt.clouddn.com/Azure_img.jpg)\n",
    "\n",
    "\n",
    "## 本周内容及学习目标\n",
    "\n",
    "本周内容聚集在复习1中的计算机视觉，以及复习2中的API操作部分，学习解决一下挑战：\n",
    "\n",
    "1. 尝试操作计算机视觉人脸识别返回[人脸识别效果](https://azure.microsoft.com/zh-cn/services/cognitive-services/face/)\n",
    "2. 阅读Azure计算机视觉的[人脸文档](https://docs.microsoft.com/zh-cn/azure/cognitive-services/face/)，以及[人脸 API v1.0文档](https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236)\n",
    "2. 观看影片[知智1分钟计算机视觉](https://zhuanlan.zhihu.com/p/35652529) 与[知智1分钟人脸识别](https://zhuanlan.zhihu.com/p/36262110)\n",
    "3. 注册Azure 使用API免费服务，获取key以为获取API应用做准备\n",
    "4. 使用requests，用代码取得API回复\n",
    "5. 写出代码，实现输入一个图片URL，可以识别出每个人脸的年龄、性别、眼镜的，加总成绩1分；列出每个人最可能的3种情绪及其判别值再加1分。\n",
    "6. 使用pandas 将返回数据用数据框展示出来。\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>\n",
       "/* 本电子讲义使用之CSS */\n",
       "div.code_cell {\n",
       "    background-color: #e5f1fe;\n",
       "}\n",
       "div.cell.selected {\n",
       "    background-color: #effee2;\n",
       "    font-size: 2rem;\n",
       "    line-height: 2.4rem;\n",
       "}\n",
       "div.cell.selected .rendered_html table {\n",
       "    font-size: 2rem !important;\n",
       "    line-height: 2.4rem !important;\n",
       "}\n",
       ".rendered_html pre code {\n",
       "    background-color: #C4E4ff;   \n",
       "    padding: 2px 25px;\n",
       "}\n",
       ".rendered_html pre {\n",
       "    background-color: #99c9ff;\n",
       "}\n",
       "div.code_cell .CodeMirror {\n",
       "    font-size: 2rem !important;\n",
       "    line-height: 2.4rem !important;\n",
       "}\n",
       ".rendered_html img, .rendered_html svg {\n",
       "    max-width: 50%;\n",
       "    height: auto;\n",
       "    float: center;\n",
       "}\n",
       "/* Gradient transparent - color - transparent */\n",
       "hr {\n",
       "    border: 0;\n",
       "    border-bottom: 1px dashed #ccc;\n",
       "}\n",
       ".emoticon{\n",
       "    font-size: 5rem;\n",
       "    line-height: 4.4rem;\n",
       "    text-align: center;\n",
       "    vertical-align: middle;\n",
       "}\n",
       "\n",
       "</style>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%html\n",
    "<style>\n",
    "/* 本电子讲义使用之CSS */\n",
    "div.code_cell {\n",
    "    background-color: #e5f1fe;\n",
    "}\n",
    "div.cell.selected {\n",
    "    background-color: #effee2;\n",
    "    font-size: 2rem;\n",
    "    line-height: 2.4rem;\n",
    "}\n",
    "div.cell.selected .rendered_html table {\n",
    "    font-size: 2rem !important;\n",
    "    line-height: 2.4rem !important;\n",
    "}\n",
    ".rendered_html pre code {\n",
    "    background-color: #C4E4ff;   \n",
    "    padding: 2px 25px;\n",
    "}\n",
    ".rendered_html pre {\n",
    "    background-color: #99c9ff;\n",
    "}\n",
    "div.code_cell .CodeMirror {\n",
    "    font-size: 2rem !important;\n",
    "    line-height: 2.4rem !important;\n",
    "}\n",
    ".rendered_html img, .rendered_html svg {\n",
    "    max-width: 50%;\n",
    "    height: auto;\n",
    "    float: center;\n",
    "}\n",
    "/* Gradient transparent - color - transparent */\n",
    "hr {\n",
    "    border: 0;\n",
    "    border-bottom: 1px dashed #ccc;\n",
    "}\n",
    ".emoticon{\n",
    "    font-size: 5rem;\n",
    "    line-height: 4.4rem;\n",
    "    text-align: center;\n",
    "    vertical-align: middle;\n",
    "}\n",
    "\n",
    "</style>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 教不完所有API，但可以学如何读、用API文件\n",
    "\n",
    "## Review: URLs for API documentation\n",
    "\n",
    "* Symbol ? 标记\n",
    "* Base url 目标API的url\n",
    "* Directories 目录, 功能\n",
    "    * [face++ detect API](https://console.faceplusplus.com.cn/documents/4888373) \n",
    "        * [https://api-cn.faceplusplus.com/facepp/v3/detect?](https://api-cn.faceplusplus.com/facepp/v3/detect)\n",
    "    * [搜索POI](https://lbs.amap.com/api/webservice/guide/api/search)\n",
    "        * [https://restapi.amap.com/v3/place/text?parameters](https://restapi.amap.com/v3/place/text?parameters)\n",
    "    * [行政区域查询](https://lbs.amap.com/api/webservice/guide/api/district)\n",
    "        * [https://restapi.amap.com/v3/config/district?parameters](https://restapi.amap.com/v3/config/district?parameters)\n",
    "       "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Review: APIs use HTTP, HTTP requests的不同模式\n",
    "\n",
    "* GET\n",
    "* POST\n",
    "* (optional) DELETE\n",
    "* e.g\n",
    "    * [https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236](https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236)\n",
    "\n",
    "\n",
    "![Requests](http://cn.python-requests.org/zh_CN/latest/_static/requests-sidebar.png)\n",
    "\n",
    "----\n",
    "\n",
    "\n",
    "request复习\n",
    "\n",
    "* [http://cn.python-requests.org/zh_CN/latest/](http://cn.python-requests.org/zh_CN/latest/)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 如果有SDK (Software Developent Kit)--> 不用自己叫HTTP requests\n",
    "\n",
    "* [Azure API for Cognitive-Face-Python](https://docs.microsoft.com/zh-cn/azure/cognitive-services/face/quickstarts/python-sdk)\n",
    "* 好处: 不用HTTP requests代码\n",
    "* 难点: SDK可能需要阅读另外的文档來叫用。若沒有SDK文档，你要自己看懂代碼"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 如果不想用，我们还是应该自己学会如何叫HTTP requests\n",
    "\n",
    "* img_url  \n",
    "\n",
    "http://newmedia.nfu.edu.cn/wcy/wp-content/uploads/2018/04/post_20180424__NFU_DoraHacks_imoji%E5%9B%A2%E9%98%9F.jpg\n",
    "\n",
    "![http://newmedia.nfu.edu.cn/wcy/wp-content/uploads/2018/04/post_20180424__NFU_DoraHacks_imoji%E5%9B%A2%E9%98%9F.jpg](http://newmedia.nfu.edu.cn/wcy/wp-content/uploads/2018/04/post_20180424__NFU_DoraHacks_imoji%E5%9B%A2%E9%98%9F.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 参考API文档\n",
    "\n",
    "* [Face API - v1.0](https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039524c)\n",
    "\n",
    "* [API URL format]\n",
    "    * Request URL [https://[location].api.cognitive.microsoft.com/face/v1.0/detect[?returnFaceId][&returnFaceLandmarks][&returnFaceAttributes]](https://[location].api.cognitive.microsoft.com/face/v1.0/detect[?returnFaceId][&returnFaceLandmarks][&returnFaceAttributes)\n",
    "    * symbol ?\n",
    "    * [location]\n",
    "    * base url  [https://[location].api.cognitive.microsoft.com/face/v1.0/detect](https://[location].api.cognitive.microsoft.com/face/v1.0/detect)\n",
    "    \n",
    "## Python Code sample\n",
    "\n",
    "\n",
    "请大家在Azure网站上面找到这两部分内容~我会找同学分享屏幕展示\n",
    "\n",
    "\n",
    "```python\n",
    "\n",
    "########### Python 3.2 #############\n",
    "import http.client, urllib.request, urllib.parse, urllib.error, base64\n",
    "\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': '{subscription key}',\n",
    "}\n",
    "\n",
    "params = urllib.parse.urlencode({\n",
    "    # Request parameters\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    'returnFaceAttributes': '{string}',\n",
    "})\n",
    "\n",
    "try:\n",
    "    conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')\n",
    "    conn.request(\"POST\", \"/face/v1.0/detect?%s\" % params, \"{body}\", headers)\n",
    "    response = conn.getresponse()\n",
    "    data = response.read()\n",
    "    print(data)\n",
    "    conn.close()\n",
    "except Exception as e:\n",
    "    print(\"[Errno {0}] {1}\".format(e.errno, e.strerror))\n",
    "\n",
    "```\n",
    "----\n",
    "\n",
    "```python\n",
    "\n",
    "import requests\n",
    "import json\n",
    "\n",
    "# set to your own subscription key value\n",
    "subscription_key = None\n",
    "assert subscription_key\n",
    "\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://<My Endpoint String>.com/face/v1.0/detect'\n",
    "\n",
    "image_url = 'https://upload.wikimedia.org/wikipedia/commons/3/37/Dagestani_man_and_woman.jpg'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 可选参数,请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "print(json.dumps(response.json()))\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 1、导入需要的requests模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 先导入为们需要的模块\n",
    "import requests\n",
    "import json"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 2、输入我们需要API网站注册的API_Key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [],
   "source": [
    "KEY = '29153ccda10844dfa502a054e03cdd54'  # Replace with a valid Subscription Key here.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 3、目标url [base url] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Base URL,  Request URL中 符号?以前\n",
    "BASE_URL = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect' \n",
    "\n",
    "# API KEY 不要用別人的\n",
    "KEY = '29153ccda10844dfa502a054e03cdd54'  # Replace with a valid Subscription Key here."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 4、沿用API文档的示范代码,准备我们的headers和图片(数据)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 沿用API的示范代碼，{subscription key}用KEY代入\n",
    "HEADERS = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': '{}'.format(KEY),\n",
    "}\n",
    "\n",
    "img_url = 'https://bkimg.cdn.bcebos.com/pic/72f082025aafa40f6d9b4785a464034f78f0190c?x-bce-process=image/resize,m_lfit,w_268,limit_1/format,f_jpg'\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 5、准备symbol ? 后面的数据,这里需要注意,一定要详细阅读API文档中的 “参数功能”,按照要求格式准备payload\n",
    "\n",
    "* 参数功能可能有:\n",
    "    * 1、是否必要?必要的一定要准备好\n",
    "    * 2、选填的一定是功能,要根据功能需求 好好填噢"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {\n",
    "    'url': '{}'.format(img_url),\n",
    "}\n",
    "payload = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'flase',\n",
    "    'returnFaceAttributes': '{}'.format('age,gender,glasses,emotion'), \n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 6、requests发送我们请求\n",
    "\n",
    "* 注意:\n",
    "    * 详细阅读文档,注意请求方式(GET、POST、DELETE)\n",
    "    * 注意json 和字典的差异 ,str vs dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': 'f00a3018-81a8-49f9-b8d9-6c6b69cf2717',\n",
       "  'faceRectangle': {'top': 67, 'left': 147, 'width': 94, 'height': 94},\n",
       "  'faceAttributes': {'gender': 'female',\n",
       "   'age': 20.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.0,\n",
       "    'neutral': 0.999,\n",
       "    'sadness': 0.001,\n",
       "    'surprise': 0.0}}}]"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 坑。参考http://docs.python-requests.org/zh_CN/latest/user/quickstart.html  【更加复杂的post请求】\n",
    "# 差別是 string 字串 vs. dict 字典\n",
    "# Azura 使用的是 data = json.dumps(payload) 或 json=payload，data = payload 会出错\n",
    "r = requests.post(BASE_URL, data=json.dumps(data), params=payload, headers=HEADERS)\n",
    "\n",
    "r.status_code\n",
    "results = r.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 拿到数据会不会很开心? 不过我们还可以做的更好\n",
    "* 别忘记我们是会基本处理数据的,至少要想到用pandas表格化数据."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>年龄</th>\n",
       "      <th>眼镜</th>\n",
       "      <th>生气</th>\n",
       "      <th>蔑视</th>\n",
       "      <th>厌恶</th>\n",
       "      <th>恐惧</th>\n",
       "      <th>高兴</th>\n",
       "      <th>平静</th>\n",
       "      <th>伤心</th>\n",
       "      <th>惊讶</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>faceId</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>f00a3018-81a8-49f9-b8d9-6c6b69cf2717</th>\n",
       "      <td>女性</td>\n",
       "      <td>20.0</td>\n",
       "      <td>没有眼镜</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.999</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                      性别    年龄    眼镜   生气   蔑视   厌恶   恐惧   高兴  \\\n",
       "faceId                                                                          \n",
       "f00a3018-81a8-49f9-b8d9-6c6b69cf2717  女性  20.0  没有眼镜  0.0  0.0  0.0  0.0  0.0   \n",
       "\n",
       "                                         平静     伤心   惊讶  \n",
       "faceId                                                   \n",
       "f00a3018-81a8-49f9-b8d9-6c6b69cf2717  0.999  0.001  0.0  "
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df_ax = pd.json_normalize(results)\n",
    "df_ax = df_ax.rename( columns = {\"faceAttributes.gender\":\"性别\",\n",
    "                                \"faceAttributes.age\":\"年龄\",\n",
    "                                \"faceAttributes.glasses\":\"眼镜\",\n",
    "                                \"faceAttributes.emotion.anger\":\"生气\",\n",
    "                                \"faceAttributes.emotion.contempt\":\"蔑视\",\n",
    "                                \"faceAttributes.emotion.disgust\":\"厌恶\",\n",
    "                                \"faceAttributes.emotion.fear\":\"恐惧\",\n",
    "                                \"faceAttributes.emotion.happiness\":\"高兴\",\n",
    "                                \"faceAttributes.emotion.neutral\":\"平静\",\n",
    "                                \"faceAttributes.emotion.sadness\":\"伤心\",\n",
    "                                \"faceAttributes.emotion.surprise\":\"惊讶\",})\n",
    "df_ax = df_ax.set_index('faceId')\n",
    "df_ax = df_ax.iloc[:,4:]\n",
    "df_ax.replace({\"male\":\"男性\",\n",
    "              \"female\":\"女性\",\n",
    "              \"NoGlasses\":\"没有眼镜\",\n",
    "              \"ReadingGlasses\":\"戴眼镜\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>年龄</th>\n",
       "      <th>眼镜</th>\n",
       "      <th>生气</th>\n",
       "      <th>蔑视</th>\n",
       "      <th>厌恶</th>\n",
       "      <th>恐惧</th>\n",
       "      <th>高兴</th>\n",
       "      <th>平静</th>\n",
       "      <th>伤心</th>\n",
       "      <th>惊讶</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>faceId</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>ff219a09-7f15-4bbe-8491-541a9587f8d7</th>\n",
       "      <td>男性</td>\n",
       "      <td>21.0</td>\n",
       "      <td>没有眼镜</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.057</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.003</td>\n",
       "      <td>0.909</td>\n",
       "      <td>0.030</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5df9f2b5-3026-42e5-a354-1832da945cb6</th>\n",
       "      <td>男性</td>\n",
       "      <td>19.0</td>\n",
       "      <td>没有眼镜</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.999</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>05a13081-8c6f-4c15-910d-54d8f5020f97</th>\n",
       "      <td>男性</td>\n",
       "      <td>22.0</td>\n",
       "      <td>没有眼镜</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.995</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>722bdfb3-d8f2-4f4d-b492-415dd5ef8184</th>\n",
       "      <td>男性</td>\n",
       "      <td>19.0</td>\n",
       "      <td>没有眼镜</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.999</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>eefd26f9-efca-4bec-96e0-a4565939bda7</th>\n",
       "      <td>男性</td>\n",
       "      <td>19.0</td>\n",
       "      <td>没有眼镜</td>\n",
       "      <td>0.004</td>\n",
       "      <td>0.095</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.023</td>\n",
       "      <td>0.841</td>\n",
       "      <td>0.036</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                      性别    年龄    眼镜     生气     蔑视     厌恶  \\\n",
       "faceId                                                                      \n",
       "ff219a09-7f15-4bbe-8491-541a9587f8d7  男性  21.0  没有眼镜  0.000  0.057  0.000   \n",
       "5df9f2b5-3026-42e5-a354-1832da945cb6  男性  19.0  没有眼镜  0.000  0.000  0.000   \n",
       "05a13081-8c6f-4c15-910d-54d8f5020f97  男性  22.0  没有眼镜  0.001  0.001  0.000   \n",
       "722bdfb3-d8f2-4f4d-b492-415dd5ef8184  男性  19.0  没有眼镜  0.000  0.000  0.000   \n",
       "eefd26f9-efca-4bec-96e0-a4565939bda7  男性  19.0  没有眼镜  0.004  0.095  0.001   \n",
       "\n",
       "                                       恐惧     高兴     平静     伤心     惊讶  \n",
       "faceId                                                                 \n",
       "ff219a09-7f15-4bbe-8491-541a9587f8d7  0.0  0.003  0.909  0.030  0.000  \n",
       "5df9f2b5-3026-42e5-a354-1832da945cb6  0.0  0.000  0.999  0.001  0.000  \n",
       "05a13081-8c6f-4c15-910d-54d8f5020f97  0.0  0.000  0.995  0.000  0.002  \n",
       "722bdfb3-d8f2-4f4d-b492-415dd5ef8184  0.0  0.000  0.999  0.001  0.000  \n",
       "eefd26f9-efca-4bec-96e0-a4565939bda7  0.0  0.023  0.841  0.036  0.000  "
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "HEADERS = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': '{}'.format(KEY),\n",
    "}\n",
    "\n",
    "img_url = 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=4065222306,1775359787&fm=26&gp=0.jpg'\n",
    "data = {\n",
    "    'url': '{}'.format(img_url),\n",
    "}\n",
    "payload = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'flase',\n",
    "    'returnFaceAttributes': '{}'.format('age,gender,glasses,emotion'), \n",
    "}\n",
    "r = requests.post(BASE_URL, data=json.dumps(data), params=payload, headers=HEADERS)\n",
    "\n",
    "r.status_code\n",
    "results = r.json()\n",
    "results\n",
    "import pandas as pd\n",
    "df_ax = pd.json_normalize(results)\n",
    "df_ax = df_ax.rename( columns = {\"faceAttributes.gender\":\"性别\",\n",
    "                                \"faceAttributes.age\":\"年龄\",\n",
    "                                \"faceAttributes.glasses\":\"眼镜\",\n",
    "                                \"faceAttributes.emotion.anger\":\"生气\",\n",
    "                                \"faceAttributes.emotion.contempt\":\"蔑视\",\n",
    "                                \"faceAttributes.emotion.disgust\":\"厌恶\",\n",
    "                                \"faceAttributes.emotion.fear\":\"恐惧\",\n",
    "                                \"faceAttributes.emotion.happiness\":\"高兴\",\n",
    "                                \"faceAttributes.emotion.neutral\":\"平静\",\n",
    "                                \"faceAttributes.emotion.sadness\":\"伤心\",\n",
    "                                \"faceAttributes.emotion.surprise\":\"惊讶\",})\n",
    "df_ax = df_ax.set_index('faceId')\n",
    "df_ax = df_ax.iloc[:,4:]\n",
    "df_ax.replace({\"male\":\"男性\",\n",
    "              \"female\":\"女性\",\n",
    "              \"NoGlasses\":\"没有眼镜\",\n",
    "              \"ReadingGlasses\":\"戴眼镜\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>年龄</th>\n",
       "      <th>眼镜</th>\n",
       "      <th>生气</th>\n",
       "      <th>蔑视</th>\n",
       "      <th>厌恶</th>\n",
       "      <th>恐惧</th>\n",
       "      <th>高兴</th>\n",
       "      <th>平静</th>\n",
       "      <th>伤心</th>\n",
       "      <th>惊讶</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>faceId</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>b15cdf35-0a5a-4c1f-8c0d-dc67323ca85c</th>\n",
       "      <td>男性</td>\n",
       "      <td>21.0</td>\n",
       "      <td>没有眼镜</td>\n",
       "      <td>0.052</td>\n",
       "      <td>0.135</td>\n",
       "      <td>0.039</td>\n",
       "      <td>0.208</td>\n",
       "      <td>0.002</td>\n",
       "      <td>0.119</td>\n",
       "      <td>0.078</td>\n",
       "      <td>0.368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>59e97e12-f52d-48e8-ae91-99612b1d43b2</th>\n",
       "      <td>男性</td>\n",
       "      <td>26.0</td>\n",
       "      <td>没有眼镜</td>\n",
       "      <td>0.003</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.012</td>\n",
       "      <td>0.003</td>\n",
       "      <td>0.002</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.979</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7b4a6eaa-3dd6-4524-8737-dfaabba0d02c</th>\n",
       "      <td>男性</td>\n",
       "      <td>29.0</td>\n",
       "      <td>没有眼镜</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.014</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.334</td>\n",
       "      <td>0.119</td>\n",
       "      <td>0.532</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>154ba136-9109-428c-bc29-096ad1392d8f</th>\n",
       "      <td>男性</td>\n",
       "      <td>30.0</td>\n",
       "      <td>没有眼镜</td>\n",
       "      <td>0.006</td>\n",
       "      <td>0.004</td>\n",
       "      <td>0.002</td>\n",
       "      <td>0.113</td>\n",
       "      <td>0.002</td>\n",
       "      <td>0.103</td>\n",
       "      <td>0.003</td>\n",
       "      <td>0.766</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                      性别    年龄    眼镜     生气     蔑视     厌恶  \\\n",
       "faceId                                                                      \n",
       "b15cdf35-0a5a-4c1f-8c0d-dc67323ca85c  男性  21.0  没有眼镜  0.052  0.135  0.039   \n",
       "59e97e12-f52d-48e8-ae91-99612b1d43b2  男性  26.0  没有眼镜  0.003  0.000  0.000   \n",
       "7b4a6eaa-3dd6-4524-8737-dfaabba0d02c  男性  29.0  没有眼镜  0.000  0.000  0.000   \n",
       "154ba136-9109-428c-bc29-096ad1392d8f  男性  30.0  没有眼镜  0.006  0.004  0.002   \n",
       "\n",
       "                                         恐惧     高兴     平静     伤心     惊讶  \n",
       "faceId                                                                   \n",
       "b15cdf35-0a5a-4c1f-8c0d-dc67323ca85c  0.208  0.002  0.119  0.078  0.368  \n",
       "59e97e12-f52d-48e8-ae91-99612b1d43b2  0.012  0.003  0.002  0.000  0.979  \n",
       "7b4a6eaa-3dd6-4524-8737-dfaabba0d02c  0.014  0.000  0.334  0.119  0.532  \n",
       "154ba136-9109-428c-bc29-096ad1392d8f  0.113  0.002  0.103  0.003  0.766  "
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "HEADERS = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': '{}'.format(KEY),\n",
    "}\n",
    "\n",
    "img_url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586945725714&di=f3317bc4a0a747123291988dcb6b2e23&imgtype=0&src=http%3A%2F%2Fi1.hdslb.com%2Fbfs%2Farchive%2F14a4879949f56e81d04f198f8feec9fe8481e134.jpg'\n",
    "data = {\n",
    "    'url': '{}'.format(img_url),\n",
    "}\n",
    "payload = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'flase',\n",
    "    'returnFaceAttributes': '{}'.format('age,gender,glasses,emotion'), \n",
    "}\n",
    "r = requests.post(BASE_URL, data=json.dumps(data), params=payload, headers=HEADERS)\n",
    "\n",
    "r.status_code\n",
    "results = r.json()\n",
    "results\n",
    "import pandas as pd\n",
    "df_ax = pd.json_normalize(results)\n",
    "df_ax = df_ax.rename( columns = {\"faceAttributes.gender\":\"性别\",\n",
    "                                \"faceAttributes.age\":\"年龄\",\n",
    "                                \"faceAttributes.glasses\":\"眼镜\",\n",
    "                                \"faceAttributes.emotion.anger\":\"生气\",\n",
    "                                \"faceAttributes.emotion.contempt\":\"蔑视\",\n",
    "                                \"faceAttributes.emotion.disgust\":\"厌恶\",\n",
    "                                \"faceAttributes.emotion.fear\":\"恐惧\",\n",
    "                                \"faceAttributes.emotion.happiness\":\"高兴\",\n",
    "                                \"faceAttributes.emotion.neutral\":\"平静\",\n",
    "                                \"faceAttributes.emotion.sadness\":\"伤心\",\n",
    "                                \"faceAttributes.emotion.surprise\":\"惊讶\",})\n",
    "df_ax = df_ax.set_index('faceId')\n",
    "df_ax = df_ax.iloc[:,4:]\n",
    "df_ax.replace({\"male\":\"男性\",\n",
    "              \"female\":\"女性\",\n",
    "              \"NoGlasses\":\"没有眼镜\",\n",
    "              \"ReadingGlasses\":\"戴眼镜\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 由于我Azure免费已过期，无法给大家展示Azure，我将用face++用同样的6个步骤给大家展示\n",
    "#  face++ Detect API(面部检测) 示范6个步骤\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method Response.json of <Response [200]>>"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1、先导入为们需要的模块\n",
    "import requests\n",
    "import pandas as pd\n",
    "\n",
    "api_secret = \"wNF6ttK27a80mJeeWjsQGp0-z_em0-xh\"\n",
    "# 2、输入我们API_Key\n",
    "api_key = 'yM-xzOuQcB6s4ssvOZkKuFhCY2isLUST'  # Replace with a valid Subscription Key here.\n",
    "\n",
    "\n",
    "# 3、目标url\n",
    "# 这里也可以使用本地图片 例如：filepath =\"image/tupian.jpg\"\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'https://wxt.sinaimg.cn/thumb300/b26538a1ly1g6bxcgsmgkj20ts0jsdkm.jpg?tags=%5B%5D'\n",
    "# 4、沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5、准备symbol ? 后面的数据\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,eyestatus,smiling,emotion', \n",
    "}\n",
    "\n",
    "#  6、requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "\n",
    "r.status_code\n",
    "r.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <th>性别</th>\n",
       "      <th>年龄</th>\n",
       "      <th>笑容值</th>\n",
       "      <th>笑容</th>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_open</th>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_close</th>\n",
       "      <th>...</th>\n",
       "      <th>attributes.eyestatus.right_eye_status.dark_glasses</th>\n",
       "      <th>attributes.eyestatus.right_eye_status.occlusion</th>\n",
       "      <th>生气</th>\n",
       "      <th>厌恶</th>\n",
       "      <th>恐惧</th>\n",
       "      <th>高兴</th>\n",
       "      <th>平静</th>\n",
       "      <th>伤心</th>\n",
       "      <th>惊讶</th>\n",
       "      <th>眼镜</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>6801493e90861c254cda7033d869b5d2</th>\n",
       "      <td>103</td>\n",
       "      <td>135</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>女性</td>\n",
       "      <td>22</td>\n",
       "      <td>32.327</td>\n",
       "      <td>50.0</td>\n",
       "      <td>95.271</td>\n",
       "      <td>0.007</td>\n",
       "      <td>...</td>\n",
       "      <td>0.017</td>\n",
       "      <td>2.063</td>\n",
       "      <td>0.432</td>\n",
       "      <td>0.362</td>\n",
       "      <td>5.852</td>\n",
       "      <td>13.773</td>\n",
       "      <td>49.341</td>\n",
       "      <td>28.522</td>\n",
       "      <td>1.716</td>\n",
       "      <td>没有眼镜</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 28 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                  face_rectangle.top  face_rectangle.left  \\\n",
       "face_token                                                                  \n",
       "6801493e90861c254cda7033d869b5d2                 103                  135   \n",
       "\n",
       "                                  face_rectangle.width  face_rectangle.height  \\\n",
       "face_token                                                                      \n",
       "6801493e90861c254cda7033d869b5d2                    75                     75   \n",
       "\n",
       "                                  性别  年龄     笑容值    笑容  \\\n",
       "face_token                                               \n",
       "6801493e90861c254cda7033d869b5d2  女性  22  32.327  50.0   \n",
       "\n",
       "                                  attributes.eyestatus.left_eye_status.no_glass_eye_open  \\\n",
       "face_token                                                                                 \n",
       "6801493e90861c254cda7033d869b5d2                                             95.271        \n",
       "\n",
       "                                  attributes.eyestatus.left_eye_status.no_glass_eye_close  \\\n",
       "face_token                                                                                  \n",
       "6801493e90861c254cda7033d869b5d2                                              0.007         \n",
       "\n",
       "                                  ...  \\\n",
       "face_token                        ...   \n",
       "6801493e90861c254cda7033d869b5d2  ...   \n",
       "\n",
       "                                  attributes.eyestatus.right_eye_status.dark_glasses  \\\n",
       "face_token                                                                             \n",
       "6801493e90861c254cda7033d869b5d2                                              0.017    \n",
       "\n",
       "                                  attributes.eyestatus.right_eye_status.occlusion  \\\n",
       "face_token                                                                          \n",
       "6801493e90861c254cda7033d869b5d2                                            2.063   \n",
       "\n",
       "                                     生气     厌恶     恐惧      高兴      平静      伤心  \\\n",
       "face_token                                                                      \n",
       "6801493e90861c254cda7033d869b5d2  0.432  0.362  5.852  13.773  49.341  28.522   \n",
       "\n",
       "                                     惊讶    眼镜  \n",
       "face_token                                     \n",
       "6801493e90861c254cda7033d869b5d2  1.716  没有眼镜  \n",
       "\n",
       "[1 rows x 28 columns]"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json()\n",
    "results\n",
    "\n",
    "face_pd = pd.json_normalize(results,record_path='faces')\n",
    "\n",
    "face_pd = face_pd.rename( columns = {\"attributes.gender.value\":\"性别\",\n",
    "                                \"attributes.age.value\":\"年龄\",\n",
    "                                \"attributes.glass.value\":\"眼镜\",\n",
    "                                \"attributes.smile.value\":\"笑容值\",\n",
    "                                \"attributes.smile.threshold\":\"笑容\",\n",
    "                                \"attributes.emotion.anger\":\"生气\",\n",
    "                                \"attributes.emotion.disgust\":\"厌恶\",\n",
    "                                \"attributes.emotion.fear\":\"恐惧\",\n",
    "                                \"attributes.emotion.happiness\":\"高兴\",\n",
    "                                \"attributes.emotion.neutral\":\"平静\",\n",
    "                                \"attributes.emotion.sadness\":\"伤心\",\n",
    "                                \"attributes.emotion.surprise\":\"惊讶\",})\n",
    "face_pd = face_pd.set_index('face_token')\n",
    "face_pd.replace({\"Male\":\"男性\",\n",
    "              \"Female\":\"女性\",\n",
    "              \"None\":\"没有眼镜\",\n",
    "              \"Dark\":\"戴墨镜\",\n",
    "                 \"Normal\":\"戴普通眼镜\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <th>性别</th>\n",
       "      <th>年龄</th>\n",
       "      <th>笑容值</th>\n",
       "      <th>笑容</th>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_open</th>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_close</th>\n",
       "      <th>...</th>\n",
       "      <th>attributes.eyestatus.right_eye_status.dark_glasses</th>\n",
       "      <th>attributes.eyestatus.right_eye_status.occlusion</th>\n",
       "      <th>生气</th>\n",
       "      <th>厌恶</th>\n",
       "      <th>恐惧</th>\n",
       "      <th>高兴</th>\n",
       "      <th>平静</th>\n",
       "      <th>伤心</th>\n",
       "      <th>惊讶</th>\n",
       "      <th>眼镜</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0c7c007b60df0dbec33f1f030aa5d101</th>\n",
       "      <td>140</td>\n",
       "      <td>339</td>\n",
       "      <td>120</td>\n",
       "      <td>120</td>\n",
       "      <td>女性</td>\n",
       "      <td>26</td>\n",
       "      <td>0.022</td>\n",
       "      <td>50.0</td>\n",
       "      <td>99.962</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.012</td>\n",
       "      <td>4.090</td>\n",
       "      <td>0.028</td>\n",
       "      <td>0.017</td>\n",
       "      <td>0.079</td>\n",
       "      <td>0.017</td>\n",
       "      <td>0.323</td>\n",
       "      <td>95.446</td>\n",
       "      <td>没有眼镜</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>61c14496fe7e988a52e0e15c02203706</th>\n",
       "      <td>328</td>\n",
       "      <td>624</td>\n",
       "      <td>120</td>\n",
       "      <td>120</td>\n",
       "      <td>男性</td>\n",
       "      <td>22</td>\n",
       "      <td>0.001</td>\n",
       "      <td>50.0</td>\n",
       "      <td>99.937</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.005</td>\n",
       "      <td>83.902</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.005</td>\n",
       "      <td>16.077</td>\n",
       "      <td>0.008</td>\n",
       "      <td>没有眼镜</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7ebe666e24acf40c23361bd69db1539e</th>\n",
       "      <td>118</td>\n",
       "      <td>495</td>\n",
       "      <td>117</td>\n",
       "      <td>117</td>\n",
       "      <td>女性</td>\n",
       "      <td>36</td>\n",
       "      <td>0.024</td>\n",
       "      <td>50.0</td>\n",
       "      <td>99.992</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.001</td>\n",
       "      <td>5.107</td>\n",
       "      <td>33.437</td>\n",
       "      <td>0.006</td>\n",
       "      <td>0.026</td>\n",
       "      <td>0.006</td>\n",
       "      <td>0.076</td>\n",
       "      <td>61.342</td>\n",
       "      <td>没有眼镜</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>597c04c950a54fe0fe64c5e75027bf30</th>\n",
       "      <td>115</td>\n",
       "      <td>726</td>\n",
       "      <td>92</td>\n",
       "      <td>92</td>\n",
       "      <td>女性</td>\n",
       "      <td>44</td>\n",
       "      <td>0.551</td>\n",
       "      <td>50.0</td>\n",
       "      <td>94.974</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.014</td>\n",
       "      <td>0.002</td>\n",
       "      <td>0.012</td>\n",
       "      <td>0.189</td>\n",
       "      <td>1.358</td>\n",
       "      <td>0.512</td>\n",
       "      <td>0.042</td>\n",
       "      <td>0.060</td>\n",
       "      <td>97.828</td>\n",
       "      <td>没有眼镜</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4 rows × 28 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                  face_rectangle.top  face_rectangle.left  \\\n",
       "face_token                                                                  \n",
       "0c7c007b60df0dbec33f1f030aa5d101                 140                  339   \n",
       "61c14496fe7e988a52e0e15c02203706                 328                  624   \n",
       "7ebe666e24acf40c23361bd69db1539e                 118                  495   \n",
       "597c04c950a54fe0fe64c5e75027bf30                 115                  726   \n",
       "\n",
       "                                  face_rectangle.width  face_rectangle.height  \\\n",
       "face_token                                                                      \n",
       "0c7c007b60df0dbec33f1f030aa5d101                   120                    120   \n",
       "61c14496fe7e988a52e0e15c02203706                   120                    120   \n",
       "7ebe666e24acf40c23361bd69db1539e                   117                    117   \n",
       "597c04c950a54fe0fe64c5e75027bf30                    92                     92   \n",
       "\n",
       "                                  性别  年龄    笑容值    笑容  \\\n",
       "face_token                                              \n",
       "0c7c007b60df0dbec33f1f030aa5d101  女性  26  0.022  50.0   \n",
       "61c14496fe7e988a52e0e15c02203706  男性  22  0.001  50.0   \n",
       "7ebe666e24acf40c23361bd69db1539e  女性  36  0.024  50.0   \n",
       "597c04c950a54fe0fe64c5e75027bf30  女性  44  0.551  50.0   \n",
       "\n",
       "                                  attributes.eyestatus.left_eye_status.no_glass_eye_open  \\\n",
       "face_token                                                                                 \n",
       "0c7c007b60df0dbec33f1f030aa5d101                                             99.962        \n",
       "61c14496fe7e988a52e0e15c02203706                                             99.937        \n",
       "7ebe666e24acf40c23361bd69db1539e                                             99.992        \n",
       "597c04c950a54fe0fe64c5e75027bf30                                             94.974        \n",
       "\n",
       "                                  attributes.eyestatus.left_eye_status.no_glass_eye_close  \\\n",
       "face_token                                                                                  \n",
       "0c7c007b60df0dbec33f1f030aa5d101                                                0.0         \n",
       "61c14496fe7e988a52e0e15c02203706                                                0.0         \n",
       "7ebe666e24acf40c23361bd69db1539e                                                0.0         \n",
       "597c04c950a54fe0fe64c5e75027bf30                                                0.0         \n",
       "\n",
       "                                  ...  \\\n",
       "face_token                        ...   \n",
       "0c7c007b60df0dbec33f1f030aa5d101  ...   \n",
       "61c14496fe7e988a52e0e15c02203706  ...   \n",
       "7ebe666e24acf40c23361bd69db1539e  ...   \n",
       "597c04c950a54fe0fe64c5e75027bf30  ...   \n",
       "\n",
       "                                  attributes.eyestatus.right_eye_status.dark_glasses  \\\n",
       "face_token                                                                             \n",
       "0c7c007b60df0dbec33f1f030aa5d101                                              0.000    \n",
       "61c14496fe7e988a52e0e15c02203706                                              0.000    \n",
       "7ebe666e24acf40c23361bd69db1539e                                              0.000    \n",
       "597c04c950a54fe0fe64c5e75027bf30                                              0.014    \n",
       "\n",
       "                                  attributes.eyestatus.right_eye_status.occlusion  \\\n",
       "face_token                                                                          \n",
       "0c7c007b60df0dbec33f1f030aa5d101                                            0.012   \n",
       "61c14496fe7e988a52e0e15c02203706                                            0.000   \n",
       "7ebe666e24acf40c23361bd69db1539e                                            0.001   \n",
       "597c04c950a54fe0fe64c5e75027bf30                                            0.002   \n",
       "\n",
       "                                     生气      厌恶      恐惧     高兴     平静      伤心  \\\n",
       "face_token                                                                      \n",
       "0c7c007b60df0dbec33f1f030aa5d101  4.090   0.028   0.017  0.079  0.017   0.323   \n",
       "61c14496fe7e988a52e0e15c02203706  0.001   0.005  83.902  0.001  0.005  16.077   \n",
       "7ebe666e24acf40c23361bd69db1539e  5.107  33.437   0.006  0.026  0.006   0.076   \n",
       "597c04c950a54fe0fe64c5e75027bf30  0.012   0.189   1.358  0.512  0.042   0.060   \n",
       "\n",
       "                                      惊讶    眼镜  \n",
       "face_token                                      \n",
       "0c7c007b60df0dbec33f1f030aa5d101  95.446  没有眼镜  \n",
       "61c14496fe7e988a52e0e15c02203706   0.008  没有眼镜  \n",
       "7ebe666e24acf40c23361bd69db1539e  61.342  没有眼镜  \n",
       "597c04c950a54fe0fe64c5e75027bf30  97.828  没有眼镜  \n",
       "\n",
       "[4 rows x 28 columns]"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1、先导入为们需要的模块\n",
    "import requests\n",
    "import pandas as pd\n",
    "\n",
    "api_secret = \"wNF6ttK27a80mJeeWjsQGp0-z_em0-xh\"\n",
    "# 2、输入我们API_Key\n",
    "api_key = 'yM-xzOuQcB6s4ssvOZkKuFhCY2isLUST'  # Replace with a valid Subscription Key here.\n",
    "\n",
    "\n",
    "# 3、目标url\n",
    "# 这里也可以使用本地图片 例如：filepath =\"image/tupian.jpg\"\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586945725714&di=f3317bc4a0a747123291988dcb6b2e23&imgtype=0&src=http%3A%2F%2Fi1.hdslb.com%2Fbfs%2Farchive%2F14a4879949f56e81d04f198f8feec9fe8481e134.jpg'\n",
    "# 4、沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5、准备symbol ? 后面的数据\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,eyestatus,smiling,emotion', \n",
    "}\n",
    "\n",
    "#  6、requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "\n",
    "r.status_code\n",
    "r.json\n",
    "results = r.json()\n",
    "results\n",
    "\n",
    "face_pd = pd.json_normalize(results,record_path='faces')\n",
    "\n",
    "face_pd = face_pd.rename( columns = {\"attributes.gender.value\":\"性别\",\n",
    "                                \"attributes.age.value\":\"年龄\",\n",
    "                                \"attributes.glass.value\":\"眼镜\",\n",
    "                                \"attributes.smile.value\":\"笑容值\",\n",
    "                                \"attributes.smile.threshold\":\"笑容\",\n",
    "                                \"attributes.emotion.anger\":\"生气\",\n",
    "                                \"attributes.emotion.disgust\":\"厌恶\",\n",
    "                                \"attributes.emotion.fear\":\"恐惧\",\n",
    "                                \"attributes.emotion.happiness\":\"高兴\",\n",
    "                                \"attributes.emotion.neutral\":\"平静\",\n",
    "                                \"attributes.emotion.sadness\":\"伤心\",\n",
    "                                \"attributes.emotion.surprise\":\"惊讶\",})\n",
    "face_pd = face_pd.set_index('face_token')\n",
    "face_pd.replace({\"Male\":\"男性\",\n",
    "              \"Female\":\"女性\",\n",
    "              \"None\":\"没有眼镜\",\n",
    "              \"Dark\":\"戴墨镜\",\n",
    "                 \"Normal\":\"戴普通眼镜\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <th>性别</th>\n",
       "      <th>年龄</th>\n",
       "      <th>笑容值</th>\n",
       "      <th>笑容</th>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_open</th>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_close</th>\n",
       "      <th>...</th>\n",
       "      <th>attributes.eyestatus.right_eye_status.dark_glasses</th>\n",
       "      <th>attributes.eyestatus.right_eye_status.occlusion</th>\n",
       "      <th>生气</th>\n",
       "      <th>厌恶</th>\n",
       "      <th>恐惧</th>\n",
       "      <th>高兴</th>\n",
       "      <th>平静</th>\n",
       "      <th>伤心</th>\n",
       "      <th>惊讶</th>\n",
       "      <th>眼镜</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>d184210e5d5e3a44dcbbfa5ab1911f8c</th>\n",
       "      <td>232</td>\n",
       "      <td>1215</td>\n",
       "      <td>92</td>\n",
       "      <td>92</td>\n",
       "      <td>女性</td>\n",
       "      <td>20</td>\n",
       "      <td>1.648</td>\n",
       "      <td>50.0</td>\n",
       "      <td>92.967</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>19.346</td>\n",
       "      <td>80.281</td>\n",
       "      <td>0.017</td>\n",
       "      <td>0.017</td>\n",
       "      <td>0.038</td>\n",
       "      <td>0.845</td>\n",
       "      <td>3.725</td>\n",
       "      <td>95.342</td>\n",
       "      <td>0.017</td>\n",
       "      <td>没有眼镜</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c6b53649a6082458e8a80534bb4300e7</th>\n",
       "      <td>245</td>\n",
       "      <td>369</td>\n",
       "      <td>91</td>\n",
       "      <td>91</td>\n",
       "      <td>女性</td>\n",
       "      <td>24</td>\n",
       "      <td>0.006</td>\n",
       "      <td>50.0</td>\n",
       "      <td>99.750</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.106</td>\n",
       "      <td>0.661</td>\n",
       "      <td>0.002</td>\n",
       "      <td>0.023</td>\n",
       "      <td>0.002</td>\n",
       "      <td>0.018</td>\n",
       "      <td>99.825</td>\n",
       "      <td>0.042</td>\n",
       "      <td>0.087</td>\n",
       "      <td>没有眼镜</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>71b7c111ae1738a4b38c8979fee85e7a</th>\n",
       "      <td>238</td>\n",
       "      <td>167</td>\n",
       "      <td>89</td>\n",
       "      <td>89</td>\n",
       "      <td>男性</td>\n",
       "      <td>22</td>\n",
       "      <td>3.838</td>\n",
       "      <td>50.0</td>\n",
       "      <td>47.665</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.001</td>\n",
       "      <td>7.476</td>\n",
       "      <td>0.016</td>\n",
       "      <td>0.016</td>\n",
       "      <td>0.478</td>\n",
       "      <td>91.980</td>\n",
       "      <td>0.016</td>\n",
       "      <td>0.016</td>\n",
       "      <td>没有眼镜</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6e75a9d5e07db837230d6a3bdeadfc08</th>\n",
       "      <td>217</td>\n",
       "      <td>610</td>\n",
       "      <td>88</td>\n",
       "      <td>88</td>\n",
       "      <td>女性</td>\n",
       "      <td>27</td>\n",
       "      <td>0.138</td>\n",
       "      <td>50.0</td>\n",
       "      <td>99.127</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.110</td>\n",
       "      <td>0.346</td>\n",
       "      <td>0.049</td>\n",
       "      <td>0.021</td>\n",
       "      <td>0.021</td>\n",
       "      <td>0.035</td>\n",
       "      <td>75.939</td>\n",
       "      <td>23.835</td>\n",
       "      <td>0.100</td>\n",
       "      <td>没有眼镜</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>f66b226ae3dc80b1711f8357934c4039</th>\n",
       "      <td>229</td>\n",
       "      <td>919</td>\n",
       "      <td>87</td>\n",
       "      <td>87</td>\n",
       "      <td>男性</td>\n",
       "      <td>21</td>\n",
       "      <td>1.356</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.376</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.004</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.004</td>\n",
       "      <td>0.002</td>\n",
       "      <td>99.985</td>\n",
       "      <td>0.007</td>\n",
       "      <td>0.000</td>\n",
       "      <td>戴墨镜</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 28 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                  face_rectangle.top  face_rectangle.left  \\\n",
       "face_token                                                                  \n",
       "d184210e5d5e3a44dcbbfa5ab1911f8c                 232                 1215   \n",
       "c6b53649a6082458e8a80534bb4300e7                 245                  369   \n",
       "71b7c111ae1738a4b38c8979fee85e7a                 238                  167   \n",
       "6e75a9d5e07db837230d6a3bdeadfc08                 217                  610   \n",
       "f66b226ae3dc80b1711f8357934c4039                 229                  919   \n",
       "\n",
       "                                  face_rectangle.width  face_rectangle.height  \\\n",
       "face_token                                                                      \n",
       "d184210e5d5e3a44dcbbfa5ab1911f8c                    92                     92   \n",
       "c6b53649a6082458e8a80534bb4300e7                    91                     91   \n",
       "71b7c111ae1738a4b38c8979fee85e7a                    89                     89   \n",
       "6e75a9d5e07db837230d6a3bdeadfc08                    88                     88   \n",
       "f66b226ae3dc80b1711f8357934c4039                    87                     87   \n",
       "\n",
       "                                  性别  年龄    笑容值    笑容  \\\n",
       "face_token                                              \n",
       "d184210e5d5e3a44dcbbfa5ab1911f8c  女性  20  1.648  50.0   \n",
       "c6b53649a6082458e8a80534bb4300e7  女性  24  0.006  50.0   \n",
       "71b7c111ae1738a4b38c8979fee85e7a  男性  22  3.838  50.0   \n",
       "6e75a9d5e07db837230d6a3bdeadfc08  女性  27  0.138  50.0   \n",
       "f66b226ae3dc80b1711f8357934c4039  男性  21  1.356  50.0   \n",
       "\n",
       "                                  attributes.eyestatus.left_eye_status.no_glass_eye_open  \\\n",
       "face_token                                                                                 \n",
       "d184210e5d5e3a44dcbbfa5ab1911f8c                                             92.967        \n",
       "c6b53649a6082458e8a80534bb4300e7                                             99.750        \n",
       "71b7c111ae1738a4b38c8979fee85e7a                                             47.665        \n",
       "6e75a9d5e07db837230d6a3bdeadfc08                                             99.127        \n",
       "f66b226ae3dc80b1711f8357934c4039                                              0.376        \n",
       "\n",
       "                                  attributes.eyestatus.left_eye_status.no_glass_eye_close  \\\n",
       "face_token                                                                                  \n",
       "d184210e5d5e3a44dcbbfa5ab1911f8c                                                0.0         \n",
       "c6b53649a6082458e8a80534bb4300e7                                                0.0         \n",
       "71b7c111ae1738a4b38c8979fee85e7a                                                0.0         \n",
       "6e75a9d5e07db837230d6a3bdeadfc08                                                0.0         \n",
       "f66b226ae3dc80b1711f8357934c4039                                                0.0         \n",
       "\n",
       "                                  ...  \\\n",
       "face_token                        ...   \n",
       "d184210e5d5e3a44dcbbfa5ab1911f8c  ...   \n",
       "c6b53649a6082458e8a80534bb4300e7  ...   \n",
       "71b7c111ae1738a4b38c8979fee85e7a  ...   \n",
       "6e75a9d5e07db837230d6a3bdeadfc08  ...   \n",
       "f66b226ae3dc80b1711f8357934c4039  ...   \n",
       "\n",
       "                                  attributes.eyestatus.right_eye_status.dark_glasses  \\\n",
       "face_token                                                                             \n",
       "d184210e5d5e3a44dcbbfa5ab1911f8c                                             19.346    \n",
       "c6b53649a6082458e8a80534bb4300e7                                              0.106    \n",
       "71b7c111ae1738a4b38c8979fee85e7a                                              0.000    \n",
       "6e75a9d5e07db837230d6a3bdeadfc08                                              0.110    \n",
       "f66b226ae3dc80b1711f8357934c4039                                              0.004    \n",
       "\n",
       "                                  attributes.eyestatus.right_eye_status.occlusion  \\\n",
       "face_token                                                                          \n",
       "d184210e5d5e3a44dcbbfa5ab1911f8c                                           80.281   \n",
       "c6b53649a6082458e8a80534bb4300e7                                            0.661   \n",
       "71b7c111ae1738a4b38c8979fee85e7a                                            0.001   \n",
       "6e75a9d5e07db837230d6a3bdeadfc08                                            0.346   \n",
       "f66b226ae3dc80b1711f8357934c4039                                            0.001   \n",
       "\n",
       "                                     生气     厌恶     恐惧     高兴      平静      伤心  \\\n",
       "face_token                                                                     \n",
       "d184210e5d5e3a44dcbbfa5ab1911f8c  0.017  0.017  0.038  0.845   3.725  95.342   \n",
       "c6b53649a6082458e8a80534bb4300e7  0.002  0.023  0.002  0.018  99.825   0.042   \n",
       "71b7c111ae1738a4b38c8979fee85e7a  7.476  0.016  0.016  0.478  91.980   0.016   \n",
       "6e75a9d5e07db837230d6a3bdeadfc08  0.049  0.021  0.021  0.035  75.939  23.835   \n",
       "f66b226ae3dc80b1711f8357934c4039  0.000  0.000  0.004  0.002  99.985   0.007   \n",
       "\n",
       "                                     惊讶    眼镜  \n",
       "face_token                                     \n",
       "d184210e5d5e3a44dcbbfa5ab1911f8c  0.017  没有眼镜  \n",
       "c6b53649a6082458e8a80534bb4300e7  0.087  没有眼镜  \n",
       "71b7c111ae1738a4b38c8979fee85e7a  0.016  没有眼镜  \n",
       "6e75a9d5e07db837230d6a3bdeadfc08  0.100  没有眼镜  \n",
       "f66b226ae3dc80b1711f8357934c4039  0.000   戴墨镜  \n",
       "\n",
       "[5 rows x 28 columns]"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1、先导入为们需要的模块\n",
    "import requests\n",
    "import pandas as pd\n",
    "\n",
    "api_secret = \"wNF6ttK27a80mJeeWjsQGp0-z_em0-xh\"\n",
    "# 2、输入我们API_Key\n",
    "api_key = 'yM-xzOuQcB6s4ssvOZkKuFhCY2isLUST'  # Replace with a valid Subscription Key here.\n",
    "\n",
    "\n",
    "# 3、目标url\n",
    "# 这里也可以使用本地图片 例如：filepath =\"image/tupian.jpg\"\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586946702204&di=cd3d6d211b88fa07e83ff117cf8208c1&imgtype=0&src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farchive%2F1e18eb3d02664816ff9357efafd82fec150e1d7a.jpg'\n",
    "# 4、沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5、准备symbol ? 后面的数据\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,eyestatus,smiling,emotion', \n",
    "}\n",
    "\n",
    "#  6、requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "\n",
    "r.status_code\n",
    "r.json\n",
    "results = r.json()\n",
    "results\n",
    "\n",
    "face_pd = pd.json_normalize(results,record_path='faces')\n",
    "\n",
    "face_pd = face_pd.rename( columns = {\"attributes.gender.value\":\"性别\",\n",
    "                                \"attributes.age.value\":\"年龄\",\n",
    "                                \"attributes.glass.value\":\"眼镜\",\n",
    "                                \"attributes.smile.value\":\"笑容值\",\n",
    "                                \"attributes.smile.threshold\":\"笑容\",\n",
    "                                \"attributes.emotion.anger\":\"生气\",\n",
    "                                \"attributes.emotion.disgust\":\"厌恶\",\n",
    "                                \"attributes.emotion.fear\":\"恐惧\",\n",
    "                                \"attributes.emotion.happiness\":\"高兴\",\n",
    "                                \"attributes.emotion.neutral\":\"平静\",\n",
    "                                \"attributes.emotion.sadness\":\"伤心\",\n",
    "                                \"attributes.emotion.surprise\":\"惊讶\",})\n",
    "face_pd = face_pd.set_index('face_token')\n",
    "face_pd.replace({\"Male\":\"男性\",\n",
    "              \"Female\":\"女性\",\n",
    "              \"None\":\"没有眼镜\",\n",
    "              \"Dark\":\"戴墨镜\",\n",
    "                 \"Normal\":\"戴普通眼镜\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 要如何简化此数据方便使用?\n",
    "* pandas !!\n",
    "* json_normalize：\n",
    "    * [json_normalize github](https://github.com/pandas-dev/pandas/blob/v1.0.3/pandas/io/json/_normalize.py#L114-L358)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  face_rectangle (json_normalize方法) pandas黑魔法\n",
    "* 先针对这变量看观察"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "# from pandas.io.json import json_normalize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "# results['faces']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "df = pd.json_normalize(results,record_path='faces')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_token</th>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <th>attributes.age.value</th>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_open</th>\n",
       "      <th>...</th>\n",
       "      <th>attributes.eyestatus.right_eye_status.dark_glasses</th>\n",
       "      <th>attributes.eyestatus.right_eye_status.occlusion</th>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "      <th>attributes.glass.value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>6801493e90861c254cda7033d869b5d2</td>\n",
       "      <td>103</td>\n",
       "      <td>135</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>Female</td>\n",
       "      <td>22</td>\n",
       "      <td>32.327</td>\n",
       "      <td>50.0</td>\n",
       "      <td>95.271</td>\n",
       "      <td>...</td>\n",
       "      <td>0.017</td>\n",
       "      <td>2.063</td>\n",
       "      <td>0.432</td>\n",
       "      <td>0.362</td>\n",
       "      <td>5.852</td>\n",
       "      <td>13.773</td>\n",
       "      <td>49.341</td>\n",
       "      <td>28.522</td>\n",
       "      <td>1.716</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 29 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                         face_token  face_rectangle.top  face_rectangle.left  \\\n",
       "0  6801493e90861c254cda7033d869b5d2                 103                  135   \n",
       "\n",
       "   face_rectangle.width  face_rectangle.height attributes.gender.value  \\\n",
       "0                    75                     75                  Female   \n",
       "\n",
       "   attributes.age.value  attributes.smile.value  attributes.smile.threshold  \\\n",
       "0                    22                  32.327                        50.0   \n",
       "\n",
       "   attributes.eyestatus.left_eye_status.no_glass_eye_open  ...  \\\n",
       "0                                             95.271       ...   \n",
       "\n",
       "   attributes.eyestatus.right_eye_status.dark_glasses  \\\n",
       "0                                              0.017    \n",
       "\n",
       "   attributes.eyestatus.right_eye_status.occlusion  attributes.emotion.anger  \\\n",
       "0                                            2.063                     0.432   \n",
       "\n",
       "   attributes.emotion.disgust  attributes.emotion.fear  \\\n",
       "0                       0.362                    5.852   \n",
       "\n",
       "   attributes.emotion.happiness  attributes.emotion.neutral  \\\n",
       "0                        13.773                      49.341   \n",
       "\n",
       "   attributes.emotion.sadness  attributes.emotion.surprise  \\\n",
       "0                      28.522                        1.716   \n",
       "\n",
       "   attributes.glass.value  \n",
       "0                    None  \n",
       "\n",
       "[1 rows x 29 columns]"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_face_rectangle = df[['face_token','face_rectangle.top','face_rectangle.left','face_rectangle.width','face_rectangle.height']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  face_rectangle (pandas常规方法)\n",
    "* 先针对这变量看观察"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_token</th>\n",
       "      <th>face_rectangle</th>\n",
       "      <th>attributes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>6801493e90861c254cda7033d869b5d2</td>\n",
       "      <td>{'top': 103, 'left': 135, 'width': 75, 'height...</td>\n",
       "      <td>{'gender': {'value': 'Female'}, 'age': {'value...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         face_token  \\\n",
       "0  6801493e90861c254cda7033d869b5d2   \n",
       "\n",
       "                                      face_rectangle  \\\n",
       "0  {'top': 103, 'left': 135, 'width': 75, 'height...   \n",
       "\n",
       "                                          attributes  \n",
       "0  {'gender': {'value': 'Female'}, 'age': {'value...  "
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 立马试试pd.DataFrame 並观察\n",
    "faces_data = results['faces']\n",
    "df = pd.DataFrame(faces_data)\n",
    "df\n",
    "\n",
    "# attributes, face_rectangle, face_token\n",
    "# 查一下API文檔"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    {'top': 103, 'left': 135, 'width': 75, 'height...\n",
       "Name: face_rectangle, dtype: object"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取变量 (pandas cheetsheet)\n",
    "df[\"face_rectangle\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: {'top': 103, 'left': 135, 'width': 75, 'height': 75}}"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"face_rectangle\"].to_dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>height</th>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>left</th>\n",
       "      <td>135</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>top</th>\n",
       "      <td>103</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>width</th>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0\n",
       "height   75\n",
       "left    135\n",
       "top     103\n",
       "width    75"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame(df[\"face_rectangle\"].to_dict())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>height</th>\n",
       "      <th>left</th>\n",
       "      <th>top</th>\n",
       "      <th>width</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>75</td>\n",
       "      <td>135</td>\n",
       "      <td>103</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   height  left  top  width\n",
       "0      75   135  103     75"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame(df[\"face_rectangle\"].to_dict()).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_rectangle_height</th>\n",
       "      <th>face_rectangle_left</th>\n",
       "      <th>face_rectangle_top</th>\n",
       "      <th>face_rectangle_width</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>75</td>\n",
       "      <td>135</td>\n",
       "      <td>103</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   face_rectangle_height  face_rectangle_left  face_rectangle_top  \\\n",
       "0                     75                  135                 103   \n",
       "\n",
       "   face_rectangle_width  \n",
       "0                    75  "
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_rect = pd.DataFrame(df[\"face_rectangle\"].to_dict()).T\n",
    "# 欄位名稱加上 face_rectangle \n",
    "df_rect.columns = [ \"face_rectangle_\"+x for x in df_rect.columns]\n",
    "df_rect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_token</th>\n",
       "      <th>face_rectangle</th>\n",
       "      <th>attributes</th>\n",
       "      <th>face_rectangle_height</th>\n",
       "      <th>face_rectangle_left</th>\n",
       "      <th>face_rectangle_top</th>\n",
       "      <th>face_rectangle_width</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>6801493e90861c254cda7033d869b5d2</td>\n",
       "      <td>{'top': 103, 'left': 135, 'width': 75, 'height...</td>\n",
       "      <td>{'gender': {'value': 'Female'}, 'age': {'value...</td>\n",
       "      <td>75</td>\n",
       "      <td>135</td>\n",
       "      <td>103</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         face_token  \\\n",
       "0  6801493e90861c254cda7033d869b5d2   \n",
       "\n",
       "                                      face_rectangle  \\\n",
       "0  {'top': 103, 'left': 135, 'width': 75, 'height...   \n",
       "\n",
       "                                          attributes  face_rectangle_height  \\\n",
       "0  {'gender': {'value': 'Female'}, 'age': {'value...                     75   \n",
       "\n",
       "   face_rectangle_left  face_rectangle_top  face_rectangle_width  \n",
       "0                  135                 103                    75  "
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用.join合併\n",
    "df.join(df_rect)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  face_rectangle\n",
    "* 該你了\n",
    "* 如何類似手法，運用你pandas的知識，抽出 attributes的數據\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    {'gender': {'value': 'Female'}, 'age': {'value...\n",
       "Name: attributes, dtype: object"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"attributes\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "4",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32mD:\\anaconda\\lib\\site-packages\\pandas\\core\\indexes\\range.py\u001b[0m in \u001b[0;36mget_loc\u001b[1;34m(self, key, method, tolerance)\u001b[0m\n\u001b[0;32m    349\u001b[0m             \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 350\u001b[1;33m                 \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_range\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnew_key\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    351\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: 4 is not in range",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-83-ff1be086bc6e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdf\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"attributes\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'emotion'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mD:\\anaconda\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m   1766\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1767\u001b[0m             \u001b[0mmaybe_callable\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapply_if_callable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1768\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_getitem_axis\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmaybe_callable\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1769\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1770\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_is_scalar_access\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mTuple\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\anaconda\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m_getitem_axis\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m   1963\u001b[0m         \u001b[1;31m# fall thru to straight lookup\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1964\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_validate_key\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1965\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_label\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1966\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1967\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\anaconda\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m_get_label\u001b[1;34m(self, label, axis)\u001b[0m\n\u001b[0;32m    619\u001b[0m             \u001b[1;31m# but will fail when the index is not present\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    620\u001b[0m             \u001b[1;31m# see GH5667\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 621\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_xs\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlabel\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    622\u001b[0m         \u001b[1;32melif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlabel\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtuple\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlabel\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mslice\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    623\u001b[0m             \u001b[1;32mraise\u001b[0m \u001b[0mIndexingError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"no slices here, handle elsewhere\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\anaconda\\lib\\site-packages\\pandas\\core\\generic.py\u001b[0m in \u001b[0;36mxs\u001b[1;34m(self, key, axis, level, drop_level)\u001b[0m\n\u001b[0;32m   3535\u001b[0m             \u001b[0mloc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnew_index\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_loc_level\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdrop_level\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdrop_level\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   3536\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 3537\u001b[1;33m             \u001b[0mloc\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   3538\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   3539\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mloc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mndarray\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\anaconda\\lib\\site-packages\\pandas\\core\\indexes\\range.py\u001b[0m in \u001b[0;36mget_loc\u001b[1;34m(self, key, method, tolerance)\u001b[0m\n\u001b[0;32m    350\u001b[0m                 \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_range\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnew_key\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    351\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 352\u001b[1;33m                 \u001b[1;32mraise\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    353\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0msuper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmethod\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtolerance\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtolerance\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    354\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 4"
     ]
    }
   ],
   "source": [
    "df[\"attributes\"].loc[4]['emotion']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(df[\"attributes\"].loc[0]['emotion'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'float' object is not subscriptable",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-16-af72a7c61d0b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdf\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"attributes\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mloc\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'emotion'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: 'float' object is not subscriptable"
     ]
    }
   ],
   "source": [
    "df[\"attributes\"].loc[5]['emotion']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    {'gender': {'value': 'Male'}, 'age': {'value':...\n",
       "1    {'gender': {'value': 'Female'}, 'age': {'value...\n",
       "2    {'gender': {'value': 'Male'}, 'age': {'value':...\n",
       "3    {'gender': {'value': 'Female'}, 'age': {'value...\n",
       "4    {'gender': {'value': 'Female'}, 'age': {'value...\n",
       "5                                                  NaN\n",
       "Name: attributes, dtype: object"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np  # missing value np.nan\n",
    "missing_value = {\"emotion\": {'sadness': np.nan, 'neutral': np.nan, 'disgust': np.nan, 'anger': np.nan, 'surprise': np.nan, 'fear': np.nan, 'happiness': np.nan}}\n",
    "df[\"attributes\"] = df[\"attributes\"].fillna(missing_value)\n",
    "df[\"attributes\"]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    {'gender': {'value': 'Male'}, 'age': {'value':...\n",
       "1    {'gender': {'value': 'Female'}, 'age': {'value...\n",
       "2    {'gender': {'value': 'Male'}, 'age': {'value':...\n",
       "3    {'gender': {'value': 'Female'}, 'age': {'value...\n",
       "4    {'gender': {'value': 'Female'}, 'age': {'value...\n",
       "5                                                     \n",
       "Name: attributes, dtype: object"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"attributes\"].fillna(\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 {'anger': 0.059, 'disgust': 0.059, 'fear': 0.059, 'happiness': 42.275, 'neutral': 52.466, 'sadness': 4.903, 'surprise': 0.179}\n",
      "1 {'anger': 0.169, 'disgust': 7.346, 'fear': 0.137, 'happiness': 17.673, 'neutral': 8.043, 'sadness': 66.494, 'surprise': 0.137}\n",
      "2 {'anger': 0.0, 'disgust': 0.0, 'fear': 0.0, 'happiness': 99.996, 'neutral': 0.001, 'sadness': 0.002, 'surprise': 0.001}\n",
      "3 {'anger': 0.0, 'disgust': 0.001, 'fear': 0.0, 'happiness': 99.99, 'neutral': 0.009, 'sadness': 0.0, 'surprise': 0.0}\n",
      "4 {'anger': 0.001, 'disgust': 0.001, 'fear': 0.027, 'happiness': 99.933, 'neutral': 0.027, 'sadness': 0.01, 'surprise': 0.001}\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'float' object has no attribute 'get'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-20-fd62967a3348>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mdf\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"attributes\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m     \u001b[0;31m# print (i,df[\"attributes\"].loc[i][\"emotion\"])\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m     \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mdf\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"attributes\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mloc\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"emotion\"\u001b[0m\u001b[0;34m)\u001b[0m  \u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m: 'float' object has no attribute 'get'"
     ]
    }
   ],
   "source": [
    "for i in df[\"attributes\"].index:\n",
    "    # print (i,df[\"attributes\"].loc[i][\"emotion\"])\n",
    "    print (i,df[\"attributes\"].loc[i].get(\"emotion\")  )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(df[\"attributes\"].loc[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'sadness': 4.92,\n",
       "  'neutral': 52.431,\n",
       "  'disgust': 0.059,\n",
       "  'anger': 0.059,\n",
       "  'surprise': 0.179,\n",
       "  'fear': 0.059,\n",
       "  'happiness': 42.294},\n",
       " {'sadness': 66.519,\n",
       "  'neutral': 8.067,\n",
       "  'disgust': 7.28,\n",
       "  'anger': 0.169,\n",
       "  'surprise': 0.138,\n",
       "  'fear': 0.138,\n",
       "  'happiness': 17.689},\n",
       " {'sadness': 0.002,\n",
       "  'neutral': 0.001,\n",
       "  'disgust': 0.0,\n",
       "  'anger': 0.0,\n",
       "  'surprise': 0.001,\n",
       "  'fear': 0.0,\n",
       "  'happiness': 99.996},\n",
       " {'sadness': 0.0,\n",
       "  'neutral': 0.009,\n",
       "  'disgust': 0.001,\n",
       "  'anger': 0.0,\n",
       "  'surprise': 0.0,\n",
       "  'fear': 0.0,\n",
       "  'happiness': 99.99},\n",
       " {'sadness': 0.01,\n",
       "  'neutral': 0.027,\n",
       "  'disgust': 0.001,\n",
       "  'anger': 0.001,\n",
       "  'surprise': 0.001,\n",
       "  'fear': 0.027,\n",
       "  'happiness': 99.933},\n",
       " {'emotion': {'sadness': nan,\n",
       "   'neutral': nan,\n",
       "   'disgust': nan,\n",
       "   'anger': nan,\n",
       "   'surprise': nan,\n",
       "   'fear': nan,\n",
       "   'happiness': nan}}]"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[df[\"attributes\"].loc[i][\"emotion\"] if type(df[\"attributes\"].loc[i])  is dict else missing_value for i in df[\"attributes\"].index ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>attributes_emotion_sadness</th>\n",
       "      <th>attributes_emotion_neutral</th>\n",
       "      <th>attributes_emotion_disgust</th>\n",
       "      <th>attributes_emotion_anger</th>\n",
       "      <th>attributes_emotion_surprise</th>\n",
       "      <th>attributes_emotion_fear</th>\n",
       "      <th>attributes_emotion_happiness</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4.920</td>\n",
       "      <td>52.431</td>\n",
       "      <td>0.059</td>\n",
       "      <td>0.059</td>\n",
       "      <td>0.179</td>\n",
       "      <td>0.059</td>\n",
       "      <td>42.294</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>66.519</td>\n",
       "      <td>8.067</td>\n",
       "      <td>7.280</td>\n",
       "      <td>0.169</td>\n",
       "      <td>0.138</td>\n",
       "      <td>0.138</td>\n",
       "      <td>17.689</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.002</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.000</td>\n",
       "      <td>99.996</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.000</td>\n",
       "      <td>0.009</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>99.990</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.010</td>\n",
       "      <td>0.027</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.027</td>\n",
       "      <td>99.933</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   attributes_emotion_sadness  attributes_emotion_neutral  \\\n",
       "0                       4.920                      52.431   \n",
       "1                      66.519                       8.067   \n",
       "2                       0.002                       0.001   \n",
       "3                       0.000                       0.009   \n",
       "4                       0.010                       0.027   \n",
       "5                         NaN                         NaN   \n",
       "\n",
       "   attributes_emotion_disgust  attributes_emotion_anger  \\\n",
       "0                       0.059                     0.059   \n",
       "1                       7.280                     0.169   \n",
       "2                       0.000                     0.000   \n",
       "3                       0.001                     0.000   \n",
       "4                       0.001                     0.001   \n",
       "5                         NaN                       NaN   \n",
       "\n",
       "   attributes_emotion_surprise  attributes_emotion_fear  \\\n",
       "0                        0.179                    0.059   \n",
       "1                        0.138                    0.138   \n",
       "2                        0.001                    0.000   \n",
       "3                        0.000                    0.000   \n",
       "4                        0.001                    0.027   \n",
       "5                          NaN                      NaN   \n",
       "\n",
       "   attributes_emotion_happiness  \n",
       "0                        42.294  \n",
       "1                        17.689  \n",
       "2                        99.996  \n",
       "3                        99.990  \n",
       "4                        99.933  \n",
       "5                           NaN  "
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_attr_emotion = pd.DataFrame([df[\"attributes\"].loc[i][\"emotion\"] if type(df[\"attributes\"].loc[i])  is dict else missing_value['emotion'] for i in df[\"attributes\"].index ])\n",
    "# 欄位名稱加上 face_rectangle \n",
    "df_attr_emotion.columns = [ \"attributes_emotion_\"+x for x in df_attr_emotion.columns]\n",
    "df_attr_emotion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'ret': 0,\n",
       " 'msg': 'ok',\n",
       " 'data': {'image_width': 320,\n",
       "  'image_height': 180,\n",
       "  'face_list': [{'face_id': '3595841850264538331',\n",
       "    'x': 141,\n",
       "    'y': 50,\n",
       "    'width': 60,\n",
       "    'height': 60,\n",
       "    'gender': 0,\n",
       "    'age': 19,\n",
       "    'expression': 0,\n",
       "    'beauty': 96,\n",
       "    'glass': 0,\n",
       "    'pitch': 7,\n",
       "    'yaw': 8,\n",
       "    'roll': -5,\n",
       "    'face_shape': {'face_profile': [{'x': 144, 'y': 69},\n",
       "      {'x': 144, 'y': 74},\n",
       "      {'x': 144, 'y': 80},\n",
       "      {'x': 145, 'y': 85},\n",
       "      {'x': 147, 'y': 90},\n",
       "      {'x': 149, 'y': 95},\n",
       "      {'x': 153, 'y': 100},\n",
       "      {'x': 157, 'y': 103},\n",
       "      {'x': 161, 'y': 107},\n",
       "      {'x': 166, 'y': 109},\n",
       "      {'x': 171, 'y': 110},\n",
       "      {'x': 176, 'y': 108},\n",
       "      {'x': 179, 'y': 105},\n",
       "      {'x': 182, 'y': 102},\n",
       "      {'x': 185, 'y': 98},\n",
       "      {'x': 188, 'y': 94},\n",
       "      {'x': 190, 'y': 90},\n",
       "      {'x': 192, 'y': 85},\n",
       "      {'x': 193, 'y': 81},\n",
       "      {'x': 194, 'y': 76},\n",
       "      {'x': 194, 'y': 72}],\n",
       "     'left_eye': [{'x': 155, 'y': 69},\n",
       "      {'x': 158, 'y': 71},\n",
       "      {'x': 160, 'y': 72},\n",
       "      {'x': 163, 'y': 72},\n",
       "      {'x': 166, 'y': 71},\n",
       "      {'x': 164, 'y': 69},\n",
       "      {'x': 161, 'y': 67},\n",
       "      {'x': 158, 'y': 67}],\n",
       "     'right_eye': [{'x': 189, 'y': 72},\n",
       "      {'x': 187, 'y': 73},\n",
       "      {'x': 185, 'y': 74},\n",
       "      {'x': 183, 'y': 73},\n",
       "      {'x': 181, 'y': 72},\n",
       "      {'x': 183, 'y': 70},\n",
       "      {'x': 185, 'y': 69},\n",
       "      {'x': 188, 'y': 70}],\n",
       "     'left_eyebrow': [{'x': 152, 'y': 62},\n",
       "      {'x': 156, 'y': 62},\n",
       "      {'x': 161, 'y': 62},\n",
       "      {'x': 165, 'y': 63},\n",
       "      {'x': 169, 'y': 64},\n",
       "      {'x': 166, 'y': 61},\n",
       "      {'x': 161, 'y': 59},\n",
       "      {'x': 156, 'y': 59}],\n",
       "     'right_eyebrow': [{'x': 193, 'y': 65},\n",
       "      {'x': 190, 'y': 64},\n",
       "      {'x': 186, 'y': 64},\n",
       "      {'x': 183, 'y': 64},\n",
       "      {'x': 180, 'y': 64},\n",
       "      {'x': 183, 'y': 62},\n",
       "      {'x': 187, 'y': 62},\n",
       "      {'x': 190, 'y': 62}],\n",
       "     'mouth': [{'x': 164, 'y': 97},\n",
       "      {'x': 166, 'y': 99},\n",
       "      {'x': 169, 'y': 100},\n",
       "      {'x': 172, 'y': 101},\n",
       "      {'x': 174, 'y': 100},\n",
       "      {'x': 177, 'y': 99},\n",
       "      {'x': 178, 'y': 97},\n",
       "      {'x': 177, 'y': 95},\n",
       "      {'x': 174, 'y': 93},\n",
       "      {'x': 172, 'y': 94},\n",
       "      {'x': 170, 'y': 93},\n",
       "      {'x': 167, 'y': 95},\n",
       "      {'x': 167, 'y': 97},\n",
       "      {'x': 169, 'y': 97},\n",
       "      {'x': 172, 'y': 97},\n",
       "      {'x': 174, 'y': 97},\n",
       "      {'x': 176, 'y': 97},\n",
       "      {'x': 176, 'y': 97},\n",
       "      {'x': 174, 'y': 96},\n",
       "      {'x': 172, 'y': 96},\n",
       "      {'x': 169, 'y': 96},\n",
       "      {'x': 167, 'y': 96}],\n",
       "     'nose': [{'x': 174, 'y': 85},\n",
       "      {'x': 174, 'y': 71},\n",
       "      {'x': 172, 'y': 75},\n",
       "      {'x': 170, 'y': 78},\n",
       "      {'x': 168, 'y': 82},\n",
       "      {'x': 167, 'y': 86},\n",
       "      {'x': 170, 'y': 88},\n",
       "      {'x': 174, 'y': 89},\n",
       "      {'x': 177, 'y': 88},\n",
       "      {'x': 180, 'y': 86},\n",
       "      {'x': 178, 'y': 83},\n",
       "      {'x': 177, 'y': 79},\n",
       "      {'x': 175, 'y': 75}]}}]}}"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import hashlib  \n",
    "import time  \n",
    "import random  \n",
    "import string\n",
    "import requests  \n",
    "import base64  \n",
    "import requests\n",
    "import numpy as np\n",
    "from urllib.parse import urlencode\n",
    "import json #用于post后得到的字符串到字典的转换\n",
    "\n",
    "app_id = '2131774151' \n",
    "app_key = 'vA91kKiCgF9JJDfT'\n",
    "\n",
    "\n",
    "def get_params(img):                         #鉴权计算并返回请求参数\n",
    "    #请求时间戳（秒级），用于防止请求重放（保证签名5分钟有效\n",
    "    time_stamp=str(int(time.time())) \n",
    "    #请求随机字符串，用于保证签名不可预测,16代表16位\n",
    "    nonce_str = ''.join(random.sample(string.ascii_letters + string.digits, 16))\n",
    "\n",
    "    params = {'app_id':app_id,                #请求包，需要根据不同的任务修改，基本相同\n",
    "              'image':img,                    #文字类的任务可能是‘text’，由主函数传递进来\n",
    "              'mode':'0' ,                    #身份证件类可能是'card_type'\n",
    "              'time_stamp':time_stamp,        #时间戳，都一样\n",
    "              'nonce_str':nonce_str,          #随机字符串，都一样\n",
    "              #'sign':''                      #签名不参与鉴权计算，只是列出来示意\n",
    "             }\n",
    "\n",
    "    sort_dict= sorted(params.items(), key=lambda item:item[0], reverse = False)  #字典排序\n",
    "    sort_dict.append(('app_key',app_key))   #尾部添加appkey\n",
    "    rawtext= urlencode(sort_dict).encode()  #urlencod编码\n",
    "    sha = hashlib.md5()    \n",
    "    sha.update(rawtext)\n",
    "    md5text= sha.hexdigest().upper()        #MD5加密计算\n",
    "    params['sign']=md5text                  #将签名赋值到sign\n",
    "    return  params                          #返回请求包\n",
    "\n",
    "with open(r\"C:\\Users\\ASUS\\Desktop\\yuyan.jpg\",\"rb\") as f:\n",
    "    base64_data = base64.b64encode(f.read())\n",
    "\n",
    "\n",
    "params = get_params(base64_data)    #获取鉴权签名并获取请求参数\n",
    "\n",
    "url = \"https://api.ai.qq.com/fcgi-bin/face/face_detectface\"  # 人脸分析\n",
    "    #检测给定图片（Image）中的所有人脸（Face）的位置和相应的面部属性。位置包括（x, y, w, h），面部属性包括性别（gender）, 年龄（age）, 表情（expression）, 魅力（beauty）, 眼镜（glass）和姿态（pitch，roll，yaw）   \n",
    "res = requests.post(url,params).json()\n",
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'ret': 0,\n",
       " 'msg': 'ok',\n",
       " 'data': {'image_width': 1440,\n",
       "  'image_height': 900,\n",
       "  'face_list': [{'face_id': '3595842131307054221',\n",
       "    'x': 346,\n",
       "    'y': 226,\n",
       "    'width': 117,\n",
       "    'height': 117,\n",
       "    'gender': 97,\n",
       "    'age': 16,\n",
       "    'expression': 26,\n",
       "    'beauty': 78,\n",
       "    'glass': 0,\n",
       "    'pitch': 3,\n",
       "    'yaw': -20,\n",
       "    'roll': 32,\n",
       "    'face_shape': {'face_profile': [{'x': 367, 'y': 287},\n",
       "      {'x': 369, 'y': 294},\n",
       "      {'x': 373, 'y': 301},\n",
       "      {'x': 377, 'y': 307},\n",
       "      {'x': 382, 'y': 312},\n",
       "      {'x': 388, 'y': 318},\n",
       "      {'x': 393, 'y': 323},\n",
       "      {'x': 399, 'y': 328},\n",
       "      {'x': 405, 'y': 334},\n",
       "      {'x': 411, 'y': 338},\n",
       "      {'x': 418, 'y': 339},\n",
       "      {'x': 428, 'y': 335},\n",
       "      {'x': 436, 'y': 327},\n",
       "      {'x': 443, 'y': 318},\n",
       "      {'x': 449, 'y': 308},\n",
       "      {'x': 452, 'y': 298},\n",
       "      {'x': 453, 'y': 287},\n",
       "      {'x': 451, 'y': 275},\n",
       "      {'x': 448, 'y': 265},\n",
       "      {'x': 443, 'y': 254},\n",
       "      {'x': 439, 'y': 245}],\n",
       "     'left_eye': [{'x': 368, 'y': 285},\n",
       "      {'x': 371, 'y': 285},\n",
       "      {'x': 374, 'y': 284},\n",
       "      {'x': 376, 'y': 282},\n",
       "      {'x': 377, 'y': 280},\n",
       "      {'x': 374, 'y': 279},\n",
       "      {'x': 371, 'y': 280},\n",
       "      {'x': 369, 'y': 282}],\n",
       "     'right_eye': [{'x': 408, 'y': 260},\n",
       "      {'x': 406, 'y': 264},\n",
       "      {'x': 402, 'y': 267},\n",
       "      {'x': 398, 'y': 269},\n",
       "      {'x': 394, 'y': 270},\n",
       "      {'x': 396, 'y': 265},\n",
       "      {'x': 399, 'y': 263},\n",
       "      {'x': 404, 'y': 261}],\n",
       "     'left_eyebrow': [{'x': 359, 'y': 279},\n",
       "      {'x': 362, 'y': 276},\n",
       "      {'x': 365, 'y': 275},\n",
       "      {'x': 368, 'y': 273},\n",
       "      {'x': 372, 'y': 271},\n",
       "      {'x': 367, 'y': 269},\n",
       "      {'x': 362, 'y': 270},\n",
       "      {'x': 358, 'y': 274}],\n",
       "     'right_eyebrow': [{'x': 414, 'y': 245},\n",
       "      {'x': 406, 'y': 249},\n",
       "      {'x': 398, 'y': 253},\n",
       "      {'x': 391, 'y': 259},\n",
       "      {'x': 384, 'y': 263},\n",
       "      {'x': 388, 'y': 255},\n",
       "      {'x': 395, 'y': 249},\n",
       "      {'x': 404, 'y': 245}],\n",
       "     'mouth': [{'x': 395, 'y': 321},\n",
       "      {'x': 398, 'y': 323},\n",
       "      {'x': 402, 'y': 323},\n",
       "      {'x': 406, 'y': 322},\n",
       "      {'x': 410, 'y': 319},\n",
       "      {'x': 414, 'y': 314},\n",
       "      {'x': 415, 'y': 309},\n",
       "      {'x': 408, 'y': 308},\n",
       "      {'x': 400, 'y': 308},\n",
       "      {'x': 397, 'y': 311},\n",
       "      {'x': 394, 'y': 312},\n",
       "      {'x': 393, 'y': 316},\n",
       "      {'x': 397, 'y': 319},\n",
       "      {'x': 399, 'y': 317},\n",
       "      {'x': 401, 'y': 316},\n",
       "      {'x': 406, 'y': 313},\n",
       "      {'x': 410, 'y': 311},\n",
       "      {'x': 410, 'y': 311},\n",
       "      {'x': 406, 'y': 313},\n",
       "      {'x': 401, 'y': 316},\n",
       "      {'x': 399, 'y': 317},\n",
       "      {'x': 397, 'y': 319}],\n",
       "     'nose': [{'x': 386, 'y': 297},\n",
       "      {'x': 385, 'y': 277},\n",
       "      {'x': 384, 'y': 283},\n",
       "      {'x': 383, 'y': 290},\n",
       "      {'x': 382, 'y': 297},\n",
       "      {'x': 384, 'y': 303},\n",
       "      {'x': 390, 'y': 304},\n",
       "      {'x': 393, 'y': 303},\n",
       "      {'x': 398, 'y': 301},\n",
       "      {'x': 404, 'y': 294},\n",
       "      {'x': 397, 'y': 289},\n",
       "      {'x': 393, 'y': 285},\n",
       "      {'x': 389, 'y': 281}]}},\n",
       "   {'face_id': '3595842136844584163',\n",
       "    'x': 595,\n",
       "    'y': 198,\n",
       "    'width': 112,\n",
       "    'height': 112,\n",
       "    'gender': 52,\n",
       "    'age': 15,\n",
       "    'expression': 27,\n",
       "    'beauty': 77,\n",
       "    'glass': 0,\n",
       "    'pitch': 4,\n",
       "    'yaw': -20,\n",
       "    'roll': 10,\n",
       "    'face_shape': {'face_profile': [{'x': 612, 'y': 240},\n",
       "      {'x': 612, 'y': 248},\n",
       "      {'x': 613, 'y': 255},\n",
       "      {'x': 615, 'y': 262},\n",
       "      {'x': 618, 'y': 269},\n",
       "      {'x': 621, 'y': 276},\n",
       "      {'x': 625, 'y': 282},\n",
       "      {'x': 628, 'y': 289},\n",
       "      {'x': 632, 'y': 295},\n",
       "      {'x': 636, 'y': 302},\n",
       "      {'x': 643, 'y': 305},\n",
       "      {'x': 653, 'y': 305},\n",
       "      {'x': 663, 'y': 300},\n",
       "      {'x': 672, 'y': 295},\n",
       "      {'x': 680, 'y': 288},\n",
       "      {'x': 687, 'y': 279},\n",
       "      {'x': 692, 'y': 270},\n",
       "      {'x': 694, 'y': 259},\n",
       "      {'x': 695, 'y': 249},\n",
       "      {'x': 695, 'y': 238},\n",
       "      {'x': 694, 'y': 228}],\n",
       "     'left_eye': [{'x': 614, 'y': 241},\n",
       "      {'x': 616, 'y': 242},\n",
       "      {'x': 619, 'y': 243},\n",
       "      {'x': 622, 'y': 242},\n",
       "      {'x': 624, 'y': 240},\n",
       "      {'x': 622, 'y': 237},\n",
       "      {'x': 619, 'y': 236},\n",
       "      {'x': 616, 'y': 238}],\n",
       "     'right_eye': [{'x': 658, 'y': 233},\n",
       "      {'x': 655, 'y': 236},\n",
       "      {'x': 651, 'y': 237},\n",
       "      {'x': 646, 'y': 238},\n",
       "      {'x': 642, 'y': 237},\n",
       "      {'x': 645, 'y': 233},\n",
       "      {'x': 649, 'y': 232},\n",
       "      {'x': 654, 'y': 231}],\n",
       "     'left_eyebrow': [{'x': 609, 'y': 231},\n",
       "      {'x': 612, 'y': 230},\n",
       "      {'x': 615, 'y': 229},\n",
       "      {'x': 619, 'y': 229},\n",
       "      {'x': 622, 'y': 229},\n",
       "      {'x': 619, 'y': 225},\n",
       "      {'x': 614, 'y': 224},\n",
       "      {'x': 610, 'y': 226}],\n",
       "     'right_eyebrow': [{'x': 667, 'y': 222},\n",
       "      {'x': 658, 'y': 222},\n",
       "      {'x': 650, 'y': 223},\n",
       "      {'x': 641, 'y': 225},\n",
       "      {'x': 633, 'y': 227},\n",
       "      {'x': 639, 'y': 220},\n",
       "      {'x': 648, 'y': 217},\n",
       "      {'x': 658, 'y': 218}],\n",
       "     'mouth': [{'x': 628, 'y': 283},\n",
       "      {'x': 630, 'y': 286},\n",
       "      {'x': 633, 'y': 288},\n",
       "      {'x': 637, 'y': 289},\n",
       "      {'x': 642, 'y': 287},\n",
       "      {'x': 647, 'y': 284},\n",
       "      {'x': 650, 'y': 280},\n",
       "      {'x': 643, 'y': 277},\n",
       "      {'x': 636, 'y': 275},\n",
       "      {'x': 633, 'y': 276},\n",
       "      {'x': 630, 'y': 276},\n",
       "      {'x': 628, 'y': 279},\n",
       "      {'x': 630, 'y': 283},\n",
       "      {'x': 632, 'y': 283},\n",
       "      {'x': 635, 'y': 282},\n",
       "      {'x': 640, 'y': 282},\n",
       "      {'x': 645, 'y': 281},\n",
       "      {'x': 645, 'y': 279},\n",
       "      {'x': 639, 'y': 279},\n",
       "      {'x': 634, 'y': 280},\n",
       "      {'x': 632, 'y': 280},\n",
       "      {'x': 630, 'y': 281}],\n",
       "     'nose': [{'x': 627, 'y': 260},\n",
       "      {'x': 631, 'y': 240},\n",
       "      {'x': 629, 'y': 246},\n",
       "      {'x': 627, 'y': 251},\n",
       "      {'x': 624, 'y': 257},\n",
       "      {'x': 623, 'y': 262},\n",
       "      {'x': 627, 'y': 265},\n",
       "      {'x': 630, 'y': 266},\n",
       "      {'x': 635, 'y': 265},\n",
       "      {'x': 641, 'y': 262},\n",
       "      {'x': 637, 'y': 256},\n",
       "      {'x': 635, 'y': 251},\n",
       "      {'x': 633, 'y': 246}]}},\n",
       "   {'face_id': '3595842142348559587',\n",
       "    'x': 913,\n",
       "    'y': 208,\n",
       "    'width': 108,\n",
       "    'height': 108,\n",
       "    'gender': 99,\n",
       "    'age': 28,\n",
       "    'expression': 23,\n",
       "    'beauty': 76,\n",
       "    'glass': 0,\n",
       "    'pitch': 16,\n",
       "    'yaw': -14,\n",
       "    'roll': 1,\n",
       "    'face_shape': {'face_profile': [{'x': 919, 'y': 244},\n",
       "      {'x': 919, 'y': 252},\n",
       "      {'x': 920, 'y': 260},\n",
       "      {'x': 922, 'y': 268},\n",
       "      {'x': 924, 'y': 276},\n",
       "      {'x': 927, 'y': 284},\n",
       "      {'x': 931, 'y': 291},\n",
       "      {'x': 935, 'y': 298},\n",
       "      {'x': 940, 'y': 305},\n",
       "      {'x': 945, 'y': 311},\n",
       "      {'x': 952, 'y': 314},\n",
       "      {'x': 962, 'y': 315},\n",
       "      {'x': 971, 'y': 311},\n",
       "      {'x': 980, 'y': 306},\n",
       "      {'x': 987, 'y': 299},\n",
       "      {'x': 993, 'y': 292},\n",
       "      {'x': 998, 'y': 283},\n",
       "      {'x': 1001, 'y': 274},\n",
       "      {'x': 1003, 'y': 264},\n",
       "      {'x': 1004, 'y': 254},\n",
       "      {'x': 1005, 'y': 246}],\n",
       "     'left_eye': [{'x': 939, 'y': 245},\n",
       "      {'x': 936, 'y': 247},\n",
       "      {'x': 932, 'y': 248},\n",
       "      {'x': 928, 'y': 248},\n",
       "      {'x': 925, 'y': 247},\n",
       "      {'x': 928, 'y': 243},\n",
       "      {'x': 932, 'y': 241},\n",
       "      {'x': 936, 'y': 242}],\n",
       "     'right_eye': [{'x': 982, 'y': 244},\n",
       "      {'x': 978, 'y': 246},\n",
       "      {'x': 973, 'y': 247},\n",
       "      {'x': 969, 'y': 247},\n",
       "      {'x': 964, 'y': 246},\n",
       "      {'x': 968, 'y': 243},\n",
       "      {'x': 972, 'y': 241},\n",
       "      {'x': 977, 'y': 242}],\n",
       "     'left_eyebrow': [{'x': 919, 'y': 234},\n",
       "      {'x': 924, 'y': 235},\n",
       "      {'x': 929, 'y': 236},\n",
       "      {'x': 934, 'y': 238},\n",
       "      {'x': 939, 'y': 239},\n",
       "      {'x': 935, 'y': 234},\n",
       "      {'x': 929, 'y': 232},\n",
       "      {'x': 924, 'y': 232}],\n",
       "     'right_eyebrow': [{'x': 993, 'y': 233},\n",
       "      {'x': 984, 'y': 235},\n",
       "      {'x': 975, 'y': 236},\n",
       "      {'x': 966, 'y': 239},\n",
       "      {'x': 958, 'y': 240},\n",
       "      {'x': 965, 'y': 234},\n",
       "      {'x': 974, 'y': 231},\n",
       "      {'x': 984, 'y': 229}],\n",
       "     'mouth': [{'x': 940, 'y': 291},\n",
       "      {'x': 943, 'y': 294},\n",
       "      {'x': 948, 'y': 297},\n",
       "      {'x': 953, 'y': 298},\n",
       "      {'x': 958, 'y': 297},\n",
       "      {'x': 963, 'y': 294},\n",
       "      {'x': 967, 'y': 290},\n",
       "      {'x': 961, 'y': 290},\n",
       "      {'x': 956, 'y': 289},\n",
       "      {'x': 952, 'y': 290},\n",
       "      {'x': 949, 'y': 289},\n",
       "      {'x': 944, 'y': 290},\n",
       "      {'x': 944, 'y': 292},\n",
       "      {'x': 948, 'y': 293},\n",
       "      {'x': 952, 'y': 293},\n",
       "      {'x': 957, 'y': 292},\n",
       "      {'x': 962, 'y': 291},\n",
       "      {'x': 962, 'y': 291},\n",
       "      {'x': 957, 'y': 292},\n",
       "      {'x': 952, 'y': 293},\n",
       "      {'x': 948, 'y': 292},\n",
       "      {'x': 944, 'y': 291}],\n",
       "     'nose': [{'x': 948, 'y': 273},\n",
       "      {'x': 952, 'y': 248},\n",
       "      {'x': 949, 'y': 254},\n",
       "      {'x': 946, 'y': 260},\n",
       "      {'x': 943, 'y': 267},\n",
       "      {'x': 940, 'y': 273},\n",
       "      {'x': 944, 'y': 277},\n",
       "      {'x': 949, 'y': 279},\n",
       "      {'x': 955, 'y': 278},\n",
       "      {'x': 962, 'y': 275},\n",
       "      {'x': 959, 'y': 268},\n",
       "      {'x': 957, 'y': 261},\n",
       "      {'x': 955, 'y': 255}]}},\n",
       "   {'face_id': '3595842147835757795',\n",
       "    'x': 1205,\n",
       "    'y': 217,\n",
       "    'width': 108,\n",
       "    'height': 108,\n",
       "    'gender': 94,\n",
       "    'age': 21,\n",
       "    'expression': 0,\n",
       "    'beauty': 68,\n",
       "    'glass': 0,\n",
       "    'pitch': 6,\n",
       "    'yaw': 7,\n",
       "    'roll': -3,\n",
       "    'face_shape': {'face_profile': [{'x': 1216, 'y': 252},\n",
       "      {'x': 1216, 'y': 262},\n",
       "      {'x': 1216, 'y': 271},\n",
       "      {'x': 1218, 'y': 281},\n",
       "      {'x': 1220, 'y': 290},\n",
       "      {'x': 1224, 'y': 299},\n",
       "      {'x': 1230, 'y': 306},\n",
       "      {'x': 1238, 'y': 313},\n",
       "      {'x': 1246, 'y': 318},\n",
       "      {'x': 1254, 'y': 322},\n",
       "      {'x': 1263, 'y': 324},\n",
       "      {'x': 1272, 'y': 322},\n",
       "      {'x': 1278, 'y': 318},\n",
       "      {'x': 1285, 'y': 313},\n",
       "      {'x': 1290, 'y': 307},\n",
       "      {'x': 1295, 'y': 301},\n",
       "      {'x': 1298, 'y': 293},\n",
       "      {'x': 1301, 'y': 286},\n",
       "      {'x': 1303, 'y': 278},\n",
       "      {'x': 1304, 'y': 270},\n",
       "      {'x': 1304, 'y': 262}],\n",
       "     'left_eye': [{'x': 1232, 'y': 252},\n",
       "      {'x': 1236, 'y': 254},\n",
       "      {'x': 1240, 'y': 254},\n",
       "      {'x': 1245, 'y': 254},\n",
       "      {'x': 1249, 'y': 253},\n",
       "      {'x': 1246, 'y': 250},\n",
       "      {'x': 1241, 'y': 249},\n",
       "      {'x': 1236, 'y': 250}],\n",
       "     'right_eye': [{'x': 1290, 'y': 257},\n",
       "      {'x': 1287, 'y': 257},\n",
       "      {'x': 1283, 'y': 256},\n",
       "      {'x': 1280, 'y': 254},\n",
       "      {'x': 1276, 'y': 252},\n",
       "      {'x': 1280, 'y': 251},\n",
       "      {'x': 1285, 'y': 251},\n",
       "      {'x': 1288, 'y': 254}],\n",
       "     'left_eyebrow': [{'x': 1222, 'y': 243},\n",
       "      {'x': 1229, 'y': 242},\n",
       "      {'x': 1237, 'y': 241},\n",
       "      {'x': 1244, 'y': 241},\n",
       "      {'x': 1252, 'y': 241},\n",
       "      {'x': 1245, 'y': 236},\n",
       "      {'x': 1236, 'y': 235},\n",
       "      {'x': 1228, 'y': 237}],\n",
       "     'right_eyebrow': [{'x': 1301, 'y': 250},\n",
       "      {'x': 1294, 'y': 249},\n",
       "      {'x': 1287, 'y': 249},\n",
       "      {'x': 1280, 'y': 250},\n",
       "      {'x': 1273, 'y': 250},\n",
       "      {'x': 1279, 'y': 245},\n",
       "      {'x': 1287, 'y': 243},\n",
       "      {'x': 1294, 'y': 245}],\n",
       "     'mouth': [{'x': 1248, 'y': 298},\n",
       "      {'x': 1252, 'y': 302},\n",
       "      {'x': 1258, 'y': 304},\n",
       "      {'x': 1264, 'y': 304},\n",
       "      {'x': 1269, 'y': 304},\n",
       "      {'x': 1274, 'y': 301},\n",
       "      {'x': 1278, 'y': 297},\n",
       "      {'x': 1274, 'y': 292},\n",
       "      {'x': 1268, 'y': 289},\n",
       "      {'x': 1263, 'y': 291},\n",
       "      {'x': 1258, 'y': 290},\n",
       "      {'x': 1253, 'y': 293},\n",
       "      {'x': 1253, 'y': 298},\n",
       "      {'x': 1258, 'y': 297},\n",
       "      {'x': 1263, 'y': 297},\n",
       "      {'x': 1268, 'y': 297},\n",
       "      {'x': 1273, 'y': 297},\n",
       "      {'x': 1273, 'y': 296},\n",
       "      {'x': 1268, 'y': 296},\n",
       "      {'x': 1263, 'y': 296},\n",
       "      {'x': 1258, 'y': 296},\n",
       "      {'x': 1253, 'y': 297}],\n",
       "     'nose': [{'x': 1265, 'y': 277},\n",
       "      {'x': 1263, 'y': 258},\n",
       "      {'x': 1260, 'y': 263},\n",
       "      {'x': 1258, 'y': 268},\n",
       "      {'x': 1255, 'y': 273},\n",
       "      {'x': 1250, 'y': 278},\n",
       "      {'x': 1257, 'y': 282},\n",
       "      {'x': 1263, 'y': 283},\n",
       "      {'x': 1268, 'y': 283},\n",
       "      {'x': 1274, 'y': 280},\n",
       "      {'x': 1271, 'y': 274},\n",
       "      {'x': 1269, 'y': 268},\n",
       "      {'x': 1266, 'y': 263}]}},\n",
       "   {'face_id': '3595842153339733138',\n",
       "    'x': 159,\n",
       "    'y': 222,\n",
       "    'width': 107,\n",
       "    'height': 107,\n",
       "    'gender': 98,\n",
       "    'age': 16,\n",
       "    'expression': 30,\n",
       "    'beauty': 73,\n",
       "    'glass': 0,\n",
       "    'pitch': 11,\n",
       "    'yaw': 3,\n",
       "    'roll': -4,\n",
       "    'face_shape': {'face_profile': [{'x': 169, 'y': 250},\n",
       "      {'x': 168, 'y': 260},\n",
       "      {'x': 169, 'y': 270},\n",
       "      {'x': 170, 'y': 279},\n",
       "      {'x': 172, 'y': 289},\n",
       "      {'x': 175, 'y': 298},\n",
       "      {'x': 180, 'y': 306},\n",
       "      {'x': 186, 'y': 313},\n",
       "      {'x': 193, 'y': 320},\n",
       "      {'x': 201, 'y': 325},\n",
       "      {'x': 210, 'y': 327},\n",
       "      {'x': 219, 'y': 325},\n",
       "      {'x': 227, 'y': 321},\n",
       "      {'x': 234, 'y': 314},\n",
       "      {'x': 240, 'y': 308},\n",
       "      {'x': 245, 'y': 300},\n",
       "      {'x': 249, 'y': 292},\n",
       "      {'x': 252, 'y': 283},\n",
       "      {'x': 254, 'y': 274},\n",
       "      {'x': 256, 'y': 265},\n",
       "      {'x': 256, 'y': 257}],\n",
       "     'left_eye': [{'x': 186, 'y': 252},\n",
       "      {'x': 190, 'y': 254},\n",
       "      {'x': 194, 'y': 256},\n",
       "      {'x': 198, 'y': 256},\n",
       "      {'x': 202, 'y': 255},\n",
       "      {'x': 199, 'y': 251},\n",
       "      {'x': 195, 'y': 250},\n",
       "      {'x': 190, 'y': 250}],\n",
       "     'right_eye': [{'x': 242, 'y': 256},\n",
       "      {'x': 238, 'y': 258},\n",
       "      {'x': 234, 'y': 258},\n",
       "      {'x': 230, 'y': 258},\n",
       "      {'x': 226, 'y': 256},\n",
       "      {'x': 229, 'y': 253},\n",
       "      {'x': 234, 'y': 252},\n",
       "      {'x': 238, 'y': 253}],\n",
       "     'left_eyebrow': [{'x': 178, 'y': 240},\n",
       "      {'x': 185, 'y': 240},\n",
       "      {'x': 192, 'y': 242},\n",
       "      {'x': 199, 'y': 243},\n",
       "      {'x': 206, 'y': 244},\n",
       "      {'x': 201, 'y': 239},\n",
       "      {'x': 193, 'y': 236},\n",
       "      {'x': 185, 'y': 236}],\n",
       "     'right_eyebrow': [{'x': 249, 'y': 246},\n",
       "      {'x': 243, 'y': 246},\n",
       "      {'x': 236, 'y': 247},\n",
       "      {'x': 230, 'y': 248},\n",
       "      {'x': 223, 'y': 248},\n",
       "      {'x': 229, 'y': 244},\n",
       "      {'x': 236, 'y': 242},\n",
       "      {'x': 243, 'y': 242}],\n",
       "     'mouth': [{'x': 197, 'y': 298},\n",
       "      {'x': 201, 'y': 302},\n",
       "      {'x': 206, 'y': 305},\n",
       "      {'x': 212, 'y': 307},\n",
       "      {'x': 217, 'y': 306},\n",
       "      {'x': 222, 'y': 304},\n",
       "      {'x': 226, 'y': 301},\n",
       "      {'x': 221, 'y': 299},\n",
       "      {'x': 217, 'y': 297},\n",
       "      {'x': 213, 'y': 298},\n",
       "      {'x': 209, 'y': 296},\n",
       "      {'x': 203, 'y': 297},\n",
       "      {'x': 202, 'y': 300},\n",
       "      {'x': 207, 'y': 301},\n",
       "      {'x': 212, 'y': 302},\n",
       "      {'x': 217, 'y': 301},\n",
       "      {'x': 221, 'y': 301},\n",
       "      {'x': 222, 'y': 301},\n",
       "      {'x': 217, 'y': 301},\n",
       "      {'x': 212, 'y': 301},\n",
       "      {'x': 207, 'y': 300},\n",
       "      {'x': 202, 'y': 299}],\n",
       "     'nose': [{'x': 214, 'y': 281},\n",
       "      {'x': 215, 'y': 256},\n",
       "      {'x': 211, 'y': 263},\n",
       "      {'x': 208, 'y': 269},\n",
       "      {'x': 205, 'y': 276},\n",
       "      {'x': 201, 'y': 282},\n",
       "      {'x': 207, 'y': 286},\n",
       "      {'x': 213, 'y': 288},\n",
       "      {'x': 219, 'y': 287},\n",
       "      {'x': 225, 'y': 284},\n",
       "      {'x': 223, 'y': 277},\n",
       "      {'x': 220, 'y': 270},\n",
       "      {'x': 217, 'y': 263}]}}]}}"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import hashlib  \n",
    "import time  \n",
    "import random  \n",
    "import string\n",
    "import requests  \n",
    "import base64  \n",
    "import requests\n",
    "import numpy as np\n",
    "from urllib.parse import urlencode\n",
    "import json #用于post后得到的字符串到字典的转换\n",
    "\n",
    "app_id = '2131774151' \n",
    "app_key = 'vA91kKiCgF9JJDfT'\n",
    "\n",
    "\n",
    "def get_params(img):                         #鉴权计算并返回请求参数\n",
    "    #请求时间戳（秒级），用于防止请求重放（保证签名5分钟有效\n",
    "    time_stamp=str(int(time.time())) \n",
    "    #请求随机字符串，用于保证签名不可预测,16代表16位\n",
    "    nonce_str = ''.join(random.sample(string.ascii_letters + string.digits, 16))\n",
    "\n",
    "    params = {'app_id':app_id,                #请求包，需要根据不同的任务修改，基本相同\n",
    "              'image':img,                    #文字类的任务可能是‘text’，由主函数传递进来\n",
    "              'mode':'0' ,                    #身份证件类可能是'card_type'\n",
    "              'time_stamp':time_stamp,        #时间戳，都一样\n",
    "              'nonce_str':nonce_str,          #随机字符串，都一样\n",
    "              #'sign':''                      #签名不参与鉴权计算，只是列出来示意\n",
    "             }\n",
    "\n",
    "    sort_dict= sorted(params.items(), key=lambda item:item[0], reverse = False)  #字典排序\n",
    "    sort_dict.append(('app_key',app_key))   #尾部添加appkey\n",
    "    rawtext= urlencode(sort_dict).encode()  #urlencod编码\n",
    "    sha = hashlib.md5()    \n",
    "    sha.update(rawtext)\n",
    "    md5text= sha.hexdigest().upper()        #MD5加密计算\n",
    "    params['sign']=md5text                  #将签名赋值到sign\n",
    "    return  params                          #返回请求包\n",
    "\n",
    "with open(r\"C:\\Users\\ASUS\\Desktop\\BB.jpg\",\"rb\") as f:\n",
    "    base64_data = base64.b64encode(f.read())\n",
    "\n",
    "\n",
    "params = get_params(base64_data)    #获取鉴权签名并获取请求参数\n",
    "\n",
    "url = \"https://api.ai.qq.com/fcgi-bin/face/face_detectface\"  # 人脸分析\n",
    "    #检测给定图片（Image）中的所有人脸（Face）的位置和相应的面部属性。位置包括（x, y, w, h），面部属性包括性别（gender）, 年龄（age）, 表情（expression）, 魅力（beauty）, 眼镜（glass）和姿态（pitch，roll，yaw）   \n",
    "res = requests.post(url,params).json()\n",
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'ret': 0,\n",
       " 'msg': 'ok',\n",
       " 'data': {'image_width': 1242,\n",
       "  'image_height': 776,\n",
       "  'face_list': [{'face_id': '3595842418255119610',\n",
       "    'x': 603,\n",
       "    'y': 304,\n",
       "    'width': 148,\n",
       "    'height': 148,\n",
       "    'gender': 80,\n",
       "    'age': 20,\n",
       "    'expression': 19,\n",
       "    'beauty': 88,\n",
       "    'glass': 0,\n",
       "    'pitch': 12,\n",
       "    'yaw': 0,\n",
       "    'roll': 11,\n",
       "    'face_shape': {'face_profile': [{'x': 616, 'y': 359},\n",
       "      {'x': 619, 'y': 371},\n",
       "      {'x': 624, 'y': 382},\n",
       "      {'x': 630, 'y': 393},\n",
       "      {'x': 637, 'y': 403},\n",
       "      {'x': 645, 'y': 412},\n",
       "      {'x': 654, 'y': 421},\n",
       "      {'x': 662, 'y': 429},\n",
       "      {'x': 671, 'y': 438},\n",
       "      {'x': 680, 'y': 444},\n",
       "      {'x': 692, 'y': 446},\n",
       "      {'x': 703, 'y': 441},\n",
       "      {'x': 711, 'y': 432},\n",
       "      {'x': 718, 'y': 421},\n",
       "      {'x': 724, 'y': 410},\n",
       "      {'x': 729, 'y': 399},\n",
       "      {'x': 734, 'y': 388},\n",
       "      {'x': 737, 'y': 376},\n",
       "      {'x': 739, 'y': 363},\n",
       "      {'x': 740, 'y': 351},\n",
       "      {'x': 739, 'y': 340}],\n",
       "     'left_eye': [{'x': 634, 'y': 359},\n",
       "      {'x': 640, 'y': 363},\n",
       "      {'x': 647, 'y': 364},\n",
       "      {'x': 653, 'y': 362},\n",
       "      {'x': 659, 'y': 359},\n",
       "      {'x': 654, 'y': 353},\n",
       "      {'x': 647, 'y': 351},\n",
       "      {'x': 639, 'y': 353}],\n",
       "     'right_eye': [{'x': 714, 'y': 344},\n",
       "      {'x': 710, 'y': 349},\n",
       "      {'x': 704, 'y': 353},\n",
       "      {'x': 697, 'y': 353},\n",
       "      {'x': 691, 'y': 352},\n",
       "      {'x': 693, 'y': 344},\n",
       "      {'x': 700, 'y': 340},\n",
       "      {'x': 707, 'y': 339}],\n",
       "     'left_eyebrow': [{'x': 623, 'y': 348},\n",
       "      {'x': 633, 'y': 348},\n",
       "      {'x': 642, 'y': 348},\n",
       "      {'x': 651, 'y': 347},\n",
       "      {'x': 660, 'y': 346},\n",
       "      {'x': 651, 'y': 342},\n",
       "      {'x': 641, 'y': 341},\n",
       "      {'x': 631, 'y': 342}],\n",
       "     'right_eyebrow': [{'x': 722, 'y': 326},\n",
       "      {'x': 712, 'y': 330},\n",
       "      {'x': 701, 'y': 334},\n",
       "      {'x': 691, 'y': 338},\n",
       "      {'x': 680, 'y': 341},\n",
       "      {'x': 688, 'y': 332},\n",
       "      {'x': 699, 'y': 326},\n",
       "      {'x': 711, 'y': 322}],\n",
       "     'mouth': [{'x': 674, 'y': 432},\n",
       "      {'x': 678, 'y': 438},\n",
       "      {'x': 685, 'y': 442},\n",
       "      {'x': 692, 'y': 442},\n",
       "      {'x': 699, 'y': 440},\n",
       "      {'x': 703, 'y': 434},\n",
       "      {'x': 705, 'y': 427},\n",
       "      {'x': 700, 'y': 422},\n",
       "      {'x': 693, 'y': 420},\n",
       "      {'x': 688, 'y': 422},\n",
       "      {'x': 683, 'y': 421},\n",
       "      {'x': 677, 'y': 426},\n",
       "      {'x': 679, 'y': 431},\n",
       "      {'x': 684, 'y': 430},\n",
       "      {'x': 690, 'y': 430},\n",
       "      {'x': 695, 'y': 429},\n",
       "      {'x': 700, 'y': 428},\n",
       "      {'x': 700, 'y': 428},\n",
       "      {'x': 695, 'y': 429},\n",
       "      {'x': 690, 'y': 430},\n",
       "      {'x': 684, 'y': 430},\n",
       "      {'x': 679, 'y': 431}],\n",
       "     'nose': [{'x': 682, 'y': 398},\n",
       "      {'x': 675, 'y': 356},\n",
       "      {'x': 673, 'y': 368},\n",
       "      {'x': 671, 'y': 380},\n",
       "      {'x': 669, 'y': 391},\n",
       "      {'x': 667, 'y': 402},\n",
       "      {'x': 675, 'y': 406},\n",
       "      {'x': 684, 'y': 407},\n",
       "      {'x': 692, 'y': 403},\n",
       "      {'x': 699, 'y': 396},\n",
       "      {'x': 694, 'y': 387},\n",
       "      {'x': 687, 'y': 377},\n",
       "      {'x': 681, 'y': 366}]}},\n",
       "   {'face_id': '3595842425235490042',\n",
       "    'x': 493,\n",
       "    'y': 103,\n",
       "    'width': 133,\n",
       "    'height': 133,\n",
       "    'gender': 12,\n",
       "    'age': 30,\n",
       "    'expression': 4,\n",
       "    'beauty': 74,\n",
       "    'glass': 0,\n",
       "    'pitch': -11,\n",
       "    'yaw': 0,\n",
       "    'roll': -5,\n",
       "    'face_shape': {'face_profile': [{'x': 506, 'y': 137},\n",
       "      {'x': 504, 'y': 149},\n",
       "      {'x': 503, 'y': 161},\n",
       "      {'x': 503, 'y': 173},\n",
       "      {'x': 505, 'y': 185},\n",
       "      {'x': 508, 'y': 196},\n",
       "      {'x': 513, 'y': 207},\n",
       "      {'x': 519, 'y': 217},\n",
       "      {'x': 527, 'y': 226},\n",
       "      {'x': 536, 'y': 233},\n",
       "      {'x': 547, 'y': 236},\n",
       "      {'x': 559, 'y': 235},\n",
       "      {'x': 569, 'y': 229},\n",
       "      {'x': 578, 'y': 221},\n",
       "      {'x': 586, 'y': 212},\n",
       "      {'x': 593, 'y': 202},\n",
       "      {'x': 598, 'y': 192},\n",
       "      {'x': 601, 'y': 180},\n",
       "      {'x': 604, 'y': 169},\n",
       "      {'x': 605, 'y': 157},\n",
       "      {'x': 604, 'y': 146}],\n",
       "     'left_eye': [{'x': 525, 'y': 138},\n",
       "      {'x': 528, 'y': 140},\n",
       "      {'x': 532, 'y': 141},\n",
       "      {'x': 536, 'y': 140},\n",
       "      {'x': 540, 'y': 139},\n",
       "      {'x': 537, 'y': 136},\n",
       "      {'x': 533, 'y': 135},\n",
       "      {'x': 528, 'y': 136}],\n",
       "     'right_eye': [{'x': 584, 'y': 143},\n",
       "      {'x': 580, 'y': 144},\n",
       "      {'x': 576, 'y': 144},\n",
       "      {'x': 571, 'y': 143},\n",
       "      {'x': 568, 'y': 141},\n",
       "      {'x': 572, 'y': 139},\n",
       "      {'x': 576, 'y': 139},\n",
       "      {'x': 581, 'y': 140}],\n",
       "     'left_eyebrow': [{'x': 517, 'y': 127},\n",
       "      {'x': 523, 'y': 124},\n",
       "      {'x': 529, 'y': 123},\n",
       "      {'x': 535, 'y': 122},\n",
       "      {'x': 541, 'y': 122},\n",
       "      {'x': 536, 'y': 117},\n",
       "      {'x': 528, 'y': 117},\n",
       "      {'x': 521, 'y': 121}],\n",
       "     'right_eyebrow': [{'x': 590, 'y': 134},\n",
       "      {'x': 584, 'y': 132},\n",
       "      {'x': 577, 'y': 130},\n",
       "      {'x': 570, 'y': 129},\n",
       "      {'x': 563, 'y': 128},\n",
       "      {'x': 569, 'y': 123},\n",
       "      {'x': 578, 'y': 124},\n",
       "      {'x': 585, 'y': 128}],\n",
       "     'mouth': [{'x': 535, 'y': 190},\n",
       "      {'x': 539, 'y': 193},\n",
       "      {'x': 545, 'y': 195},\n",
       "      {'x': 551, 'y': 196},\n",
       "      {'x': 557, 'y': 196},\n",
       "      {'x': 563, 'y': 195},\n",
       "      {'x': 567, 'y': 192},\n",
       "      {'x': 565, 'y': 181},\n",
       "      {'x': 557, 'y': 173},\n",
       "      {'x': 553, 'y': 173},\n",
       "      {'x': 548, 'y': 172},\n",
       "      {'x': 539, 'y': 179},\n",
       "      {'x': 540, 'y': 190},\n",
       "      {'x': 546, 'y': 189},\n",
       "      {'x': 552, 'y': 189},\n",
       "      {'x': 557, 'y': 190},\n",
       "      {'x': 562, 'y': 191},\n",
       "      {'x': 563, 'y': 187},\n",
       "      {'x': 558, 'y': 183},\n",
       "      {'x': 552, 'y': 180},\n",
       "      {'x': 546, 'y': 182},\n",
       "      {'x': 540, 'y': 186}],\n",
       "     'nose': [{'x': 553, 'y': 157},\n",
       "      {'x': 554, 'y': 139},\n",
       "      {'x': 550, 'y': 144},\n",
       "      {'x': 547, 'y': 149},\n",
       "      {'x': 544, 'y': 154},\n",
       "      {'x': 539, 'y': 161},\n",
       "      {'x': 546, 'y': 165},\n",
       "      {'x': 552, 'y': 166},\n",
       "      {'x': 558, 'y': 166},\n",
       "      {'x': 565, 'y': 163},\n",
       "      {'x': 562, 'y': 156},\n",
       "      {'x': 559, 'y': 150},\n",
       "      {'x': 556, 'y': 145}]}},\n",
       "   {'face_id': '3595842432064865530',\n",
       "    'x': 340,\n",
       "    'y': 137,\n",
       "    'width': 122,\n",
       "    'height': 122,\n",
       "    'gender': 85,\n",
       "    'age': 22,\n",
       "    'expression': 26,\n",
       "    'beauty': 76,\n",
       "    'glass': 0,\n",
       "    'pitch': -8,\n",
       "    'yaw': 0,\n",
       "    'roll': -18,\n",
       "    'face_shape': {'face_profile': [{'x': 372, 'y': 154},\n",
       "      {'x': 366, 'y': 165},\n",
       "      {'x': 362, 'y': 176},\n",
       "      {'x': 359, 'y': 188},\n",
       "      {'x': 357, 'y': 199},\n",
       "      {'x': 356, 'y': 211},\n",
       "      {'x': 358, 'y': 223},\n",
       "      {'x': 361, 'y': 234},\n",
       "      {'x': 367, 'y': 245},\n",
       "      {'x': 375, 'y': 253},\n",
       "      {'x': 385, 'y': 258},\n",
       "      {'x': 397, 'y': 258},\n",
       "      {'x': 407, 'y': 253},\n",
       "      {'x': 416, 'y': 247},\n",
       "      {'x': 424, 'y': 239},\n",
       "      {'x': 431, 'y': 231},\n",
       "      {'x': 437, 'y': 221},\n",
       "      {'x': 442, 'y': 211},\n",
       "      {'x': 446, 'y': 201},\n",
       "      {'x': 448, 'y': 190},\n",
       "      {'x': 449, 'y': 180}],\n",
       "     'left_eye': [{'x': 386, 'y': 156},\n",
       "      {'x': 389, 'y': 159},\n",
       "      {'x': 393, 'y': 161},\n",
       "      {'x': 397, 'y': 162},\n",
       "      {'x': 401, 'y': 162},\n",
       "      {'x': 400, 'y': 158},\n",
       "      {'x': 396, 'y': 154},\n",
       "      {'x': 390, 'y': 154}],\n",
       "     'right_eye': [{'x': 440, 'y': 175},\n",
       "      {'x': 437, 'y': 175},\n",
       "      {'x': 433, 'y': 174},\n",
       "      {'x': 429, 'y': 172},\n",
       "      {'x': 426, 'y': 169},\n",
       "      {'x': 431, 'y': 167},\n",
       "      {'x': 436, 'y': 167},\n",
       "      {'x': 439, 'y': 170}],\n",
       "     'left_eyebrow': [{'x': 382, 'y': 141},\n",
       "      {'x': 389, 'y': 141},\n",
       "      {'x': 397, 'y': 143},\n",
       "      {'x': 404, 'y': 145},\n",
       "      {'x': 411, 'y': 147},\n",
       "      {'x': 406, 'y': 140},\n",
       "      {'x': 398, 'y': 136},\n",
       "      {'x': 389, 'y': 137}],\n",
       "     'right_eyebrow': [{'x': 451, 'y': 163},\n",
       "      {'x': 445, 'y': 161},\n",
       "      {'x': 440, 'y': 159},\n",
       "      {'x': 434, 'y': 158},\n",
       "      {'x': 428, 'y': 157},\n",
       "      {'x': 434, 'y': 154},\n",
       "      {'x': 440, 'y': 155},\n",
       "      {'x': 447, 'y': 157}],\n",
       "     'mouth': [{'x': 380, 'y': 217},\n",
       "      {'x': 381, 'y': 227},\n",
       "      {'x': 385, 'y': 235},\n",
       "      {'x': 394, 'y': 239},\n",
       "      {'x': 402, 'y': 237},\n",
       "      {'x': 407, 'y': 230},\n",
       "      {'x': 411, 'y': 223},\n",
       "      {'x': 412, 'y': 214},\n",
       "      {'x': 407, 'y': 206},\n",
       "      {'x': 403, 'y': 206},\n",
       "      {'x': 398, 'y': 204},\n",
       "      {'x': 388, 'y': 209},\n",
       "      {'x': 384, 'y': 224},\n",
       "      {'x': 389, 'y': 229},\n",
       "      {'x': 396, 'y': 232},\n",
       "      {'x': 402, 'y': 230},\n",
       "      {'x': 407, 'y': 227},\n",
       "      {'x': 409, 'y': 218},\n",
       "      {'x': 406, 'y': 214},\n",
       "      {'x': 402, 'y': 211},\n",
       "      {'x': 394, 'y': 211},\n",
       "      {'x': 387, 'y': 213}],\n",
       "     'nose': [{'x': 407, 'y': 185},\n",
       "      {'x': 412, 'y': 166},\n",
       "      {'x': 408, 'y': 171},\n",
       "      {'x': 403, 'y': 176},\n",
       "      {'x': 398, 'y': 181},\n",
       "      {'x': 392, 'y': 187},\n",
       "      {'x': 398, 'y': 193},\n",
       "      {'x': 404, 'y': 195},\n",
       "      {'x': 409, 'y': 196},\n",
       "      {'x': 417, 'y': 195},\n",
       "      {'x': 416, 'y': 187},\n",
       "      {'x': 415, 'y': 180},\n",
       "      {'x': 413, 'y': 173}]}},\n",
       "   {'face_id': '3595842438843909122',\n",
       "    'x': 719,\n",
       "    'y': 106,\n",
       "    'width': 96,\n",
       "    'height': 96,\n",
       "    'gender': 99,\n",
       "    'age': 37,\n",
       "    'expression': 27,\n",
       "    'beauty': 77,\n",
       "    'glass': 0,\n",
       "    'pitch': -12,\n",
       "    'yaw': 0,\n",
       "    'roll': 18,\n",
       "    'face_shape': {'face_profile': [{'x': 727, 'y': 145},\n",
       "      {'x': 729, 'y': 154},\n",
       "      {'x': 731, 'y': 163},\n",
       "      {'x': 735, 'y': 171},\n",
       "      {'x': 739, 'y': 180},\n",
       "      {'x': 744, 'y': 187},\n",
       "      {'x': 750, 'y': 194},\n",
       "      {'x': 758, 'y': 199},\n",
       "      {'x': 766, 'y': 202},\n",
       "      {'x': 775, 'y': 203},\n",
       "      {'x': 784, 'y': 202},\n",
       "      {'x': 793, 'y': 198},\n",
       "      {'x': 800, 'y': 192},\n",
       "      {'x': 805, 'y': 184},\n",
       "      {'x': 808, 'y': 175},\n",
       "      {'x': 810, 'y': 166},\n",
       "      {'x': 809, 'y': 157},\n",
       "      {'x': 807, 'y': 147},\n",
       "      {'x': 804, 'y': 138},\n",
       "      {'x': 801, 'y': 129},\n",
       "      {'x': 797, 'y': 122}],\n",
       "     'left_eye': [{'x': 738, 'y': 141},\n",
       "      {'x': 741, 'y': 142},\n",
       "      {'x': 745, 'y': 141},\n",
       "      {'x': 748, 'y': 139},\n",
       "      {'x': 751, 'y': 136},\n",
       "      {'x': 747, 'y': 134},\n",
       "      {'x': 743, 'y': 135},\n",
       "      {'x': 739, 'y': 137}],\n",
       "     'right_eye': [{'x': 784, 'y': 126},\n",
       "      {'x': 781, 'y': 129},\n",
       "      {'x': 777, 'y': 130},\n",
       "      {'x': 773, 'y': 131},\n",
       "      {'x': 769, 'y': 130},\n",
       "      {'x': 772, 'y': 126},\n",
       "      {'x': 776, 'y': 124},\n",
       "      {'x': 780, 'y': 124}],\n",
       "     'left_eyebrow': [{'x': 730, 'y': 136},\n",
       "      {'x': 734, 'y': 133},\n",
       "      {'x': 739, 'y': 131},\n",
       "      {'x': 745, 'y': 130},\n",
       "      {'x': 750, 'y': 128},\n",
       "      {'x': 744, 'y': 126},\n",
       "      {'x': 738, 'y': 127},\n",
       "      {'x': 733, 'y': 130}],\n",
       "     'right_eyebrow': [{'x': 785, 'y': 115},\n",
       "      {'x': 779, 'y': 116},\n",
       "      {'x': 774, 'y': 117},\n",
       "      {'x': 769, 'y': 118},\n",
       "      {'x': 763, 'y': 120},\n",
       "      {'x': 767, 'y': 114},\n",
       "      {'x': 773, 'y': 111},\n",
       "      {'x': 779, 'y': 111}],\n",
       "     'mouth': [{'x': 760, 'y': 173},\n",
       "      {'x': 765, 'y': 178},\n",
       "      {'x': 770, 'y': 180},\n",
       "      {'x': 777, 'y': 180},\n",
       "      {'x': 782, 'y': 177},\n",
       "      {'x': 786, 'y': 171},\n",
       "      {'x': 787, 'y': 165},\n",
       "      {'x': 781, 'y': 161},\n",
       "      {'x': 775, 'y': 160},\n",
       "      {'x': 771, 'y': 162},\n",
       "      {'x': 766, 'y': 162},\n",
       "      {'x': 762, 'y': 167},\n",
       "      {'x': 765, 'y': 174},\n",
       "      {'x': 770, 'y': 173},\n",
       "      {'x': 774, 'y': 173},\n",
       "      {'x': 779, 'y': 171},\n",
       "      {'x': 783, 'y': 168},\n",
       "      {'x': 782, 'y': 165},\n",
       "      {'x': 777, 'y': 165},\n",
       "      {'x': 772, 'y': 166},\n",
       "      {'x': 768, 'y': 168},\n",
       "      {'x': 764, 'y': 170}],\n",
       "     'nose': [{'x': 765, 'y': 145},\n",
       "      {'x': 761, 'y': 133},\n",
       "      {'x': 760, 'y': 138},\n",
       "      {'x': 758, 'y': 143},\n",
       "      {'x': 757, 'y': 148},\n",
       "      {'x': 757, 'y': 155},\n",
       "      {'x': 763, 'y': 155},\n",
       "      {'x': 768, 'y': 154},\n",
       "      {'x': 772, 'y': 152},\n",
       "      {'x': 778, 'y': 148},\n",
       "      {'x': 772, 'y': 143},\n",
       "      {'x': 768, 'y': 140},\n",
       "      {'x': 764, 'y': 136}]}}]}}"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import hashlib  \n",
    "import time  \n",
    "import random  \n",
    "import string\n",
    "import requests  \n",
    "import base64  \n",
    "import requests\n",
    "import numpy as np\n",
    "from urllib.parse import urlencode\n",
    "import json #用于post后得到的字符串到字典的转换\n",
    "\n",
    "app_id = '2131774151' \n",
    "app_key = 'vA91kKiCgF9JJDfT'\n",
    "\n",
    "\n",
    "def get_params(img):                         #鉴权计算并返回请求参数\n",
    "    #请求时间戳（秒级），用于防止请求重放（保证签名5分钟有效\n",
    "    time_stamp=str(int(time.time())) \n",
    "    #请求随机字符串，用于保证签名不可预测,16代表16位\n",
    "    nonce_str = ''.join(random.sample(string.ascii_letters + string.digits, 16))\n",
    "\n",
    "    params = {'app_id':app_id,                #请求包，需要根据不同的任务修改，基本相同\n",
    "              'image':img,                    #文字类的任务可能是‘text’，由主函数传递进来\n",
    "              'mode':'0' ,                    #身份证件类可能是'card_type'\n",
    "              'time_stamp':time_stamp,        #时间戳，都一样\n",
    "              'nonce_str':nonce_str,          #随机字符串，都一样\n",
    "              #'sign':''                      #签名不参与鉴权计算，只是列出来示意\n",
    "             }\n",
    "\n",
    "    sort_dict= sorted(params.items(), key=lambda item:item[0], reverse = False)  #字典排序\n",
    "    sort_dict.append(('app_key',app_key))   #尾部添加appkey\n",
    "    rawtext= urlencode(sort_dict).encode()  #urlencod编码\n",
    "    sha = hashlib.md5()    \n",
    "    sha.update(rawtext)\n",
    "    md5text= sha.hexdigest().upper()        #MD5加密计算\n",
    "    params['sign']=md5text                  #将签名赋值到sign\n",
    "    return  params                          #返回请求包\n",
    "\n",
    "with open(r\"C:\\Users\\ASUS\\Desktop\\OOR.jpg\",\"rb\") as f:\n",
    "    base64_data = base64.b64encode(f.read())\n",
    "\n",
    "\n",
    "params = get_params(base64_data)    #获取鉴权签名并获取请求参数\n",
    "\n",
    "url = \"https://api.ai.qq.com/fcgi-bin/face/face_detectface\"  # 人脸分析\n",
    "    #检测给定图片（Image）中的所有人脸（Face）的位置和相应的面部属性。位置包括（x, y, w, h），面部属性包括性别（gender）, 年龄（age）, 表情（expression）, 魅力（beauty）, 眼镜（glass）和姿态（pitch，roll，yaw）   \n",
    "res = requests.post(url,params).json()\n",
    "res"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 结论\n",
    "* 在调用三家平台的人脸识别API时发现，Azure和Face++可以分析多种情绪的值，而腾讯没有，仅由expression这一参数来反馈人的笑容。\n",
    "* 在分析多张人脸时发现，Azure能够准确分析出多张人脸的性别，而Face++则多次出现误判的情况，将男性识别为女性。\n",
    "* 三家平台中，Face++能获取最多元素的参数，其次是Azure，腾讯AI最少"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
